createQuery("from UserInfoVO");为什么不是createQuery("from UserInfo");哪来的UserInfoVO类

解决方案 »

  1.   

    是个javaBean/pojo 类名为UserInfoVO
    小弟初学Hibernate,对hql不清楚,在一遍文章上看到说from后面的是一个pojo类名不是数据库中的table名称
      

  2.   

    UserInfoVO有没有对应的UserInfoVO.hbm.xml文件?
      

  3.   

    <hibernate-mapping>
    <class name="UserInfoVO" table="user">
    <id name="id" type="java.lang.Integer" column="id">
    <!-- 主键产生方式 -->
       <generator class="increment" />
    </id>
    <property  name="userName" type="java.lang.String"  length="10"/>
             <property  name="password" type="java.lang.String"
    column="password"
    length="10"/>
    <property name="popedom"type="java.lang.String"
    column="popedom"
    length="10"/>

    </class>
    </hibernate-mapping>    
    public class UserInfoVO implements Serializable{
    private Integer id;
    private String userName;
    private String password;
    private String popedom;

    public UserInfoVO(){}

    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    以下省略....
      

  4.   

    最好 把Exception 全部贴上来,不然很难看出哪里错了
      

  5.   

    你先试用Session获得Connection con 对象
    做个简单的查询,目的是测试你的hibernate 数据库连接是否正确
    如果正确在研究 UserInfoVO.hbm.xml和 UserInfoVO 你的 UserInfoVO  要写出有参数的构造方法
      

  6.   

    你的<mapping resource="UserInfo.hbm.xml" />
    那么应该是from UserInfo而不是UserInfoVO.
      

  7.   

    控制台打印:
    Hibernate: select userinfovo0_.id as id, userinfovo0_.userName as userName, userinfovo0_.password as password, userinfovo0_.popedom as popedom from user userinfovo0_
    小异: Could not execute query
      

  8.   

    from UserInfo 啊
    UserInfo这个类查
      

  9.   

    还是不知道哪错了,我把几个文件全贴不来把,大家帮忙在看看,3Q
    配置文件:
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration><session-factory>
    <property name="show_sql">true</property>
    <property name="connection.url">
    jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fileUp
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <!--映射文件-->
    <mapping resource="UserInfo.hbm.xml" />
    </session-factory>
    </hibernate-configuration>UserInfo.hbm.xml文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><hibernate-mapping>
    <class name="UserInfoVO" table="user">
    <id name="id" type="java.lang.Integer" 
    column="id">
    <!-- 主键产生方式 -->
             <generator class="increment" />
    </id>
    <property  name="userName" 
    type="java.lang.String" 
    length="10"/>
    <property  name="password" type="java.lang.String"
    column="password" length="10"/>
    <property name="popedom" type="java.lang.String"
    column="popedom" length="10"/>
    </class>
    </hibernate-mapping>    import java.util.List;
    import net.sf.hibernate.Query;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.Transaction;
    import net.sf.hibernate.cfg.Configuration;public class HibernateDemo {
    /**
     * @param args
     */
    Configuration cfg;
    SessionFactory sf;
    Session session;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    HibernateDemo demo=new HibernateDemo();
    System.out.println("size: "+demo.getUserInfo().size());
    }
    catch(Exception e){
    System.out.println("小异: "+e.getMessage());
    }
    }
             public List getUserInfo() throws Exception{
    List lt=null;
    cfg=new Configuration().configure();
    sf=cfg.buildSessionFactory();
    session=sf.openSession();
    Transaction tx=session.beginTransaction();
    Query query=session.createQuery("from UserInfoVO");
    lt=query.list();
    tx.commit();
    session.close();
    return lt;
    }
    }
    import java.io.Serializable;
    public class UserInfoVO implements Serializable{
    private Integer id;
    private String userName;
    private String password;
    private String popedom;

    public UserInfoVO(){}

    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getPopedom() {
    return popedom;
    }
    public void setPopedom(String popedom) {
    this.popedom = popedom;
    }
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    }
      

  10.   

    我也遇过这个问题:
    把<!-- 主键产生方式 -->
             <generator class="increment" />
    </id>
    中class换一下,
     <generator class="native"></generator>
      

  11.   

    把你那个UserInfoVO改成UserInfo就哦了!试试!