CJ_Finger
2016-10-21 07:49
采纳率: 60%
浏览 1.3k
已采纳

Hibernate4 org.hibernate.QueryException异常问题

各位大侠好,项目一直报错"org.hibernate.QueryException: could not resolve property: u_phone of: com.wanshang.po.Users",我仔细检查po类和映射文件,也没发现错误,请问各位大侠,这个问题是哪里错了?下面是各配置文件:

Users.java

 public class Users {
    private int id;
    private String name;
    private String phone;
    private String password;
    private String paypassword = "*";
    private String usertype = "customer";
    private float point = 0;

    public Users(){}

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getPaypassword() {
        return paypassword;
    }
    public void setPaypassword(String paypassword) {
        this.paypassword = paypassword;
    }

    public String getUsertype() {
        return usertype;
    }
    public void setUsertype(String usertype) {
        this.usertype = usertype;
    }

    public float getPoint() {
        return point;
    }
    public void setPoint(float point) {
        this.point = point;
    }

}

Users.hbm.xml

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.wanshang.po.Users" table="usersInfo">

        <!-- 用户编号 -->
        <id name="id" column="u_id">
            <generator class="identity" />
        </id>
        <!-- 用户姓名 -->
        <property name="name" column="u_name" />
        <!-- 用户手机 -->
        <property name="phone" column="u_phone" />
        <!-- 用户密码 -->
        <property name="password" column="u_password" />
        <!-- 用户支付密码 -->
        <property name="paypassword" column="u_paypassword" />
        <!-- 用户类型 -->
        <property name="usertype" column="u_usertype" />
        <!-- 用户积分 -->
        <property name="point" column="u_point" />

    </class>

</hibernate-mapping>

hibernate.cfg.xml

 <?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">

<hibernate-configuration>

    <session-factory>
        <property name="myeclipse.connection.profile">mysql</property>
        <!-- 数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 数据库驱动 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库链接 -->
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/wanshang</property>
        <!-- 数据库用户名 -->
        <property name="connection.username">root</property>
        <!-- 数据库密码 -->
        <property name="connection.password"></property>
        <!-- 数据库连接池最大连接数 -->
        <property name="connection.pool_size">2000</property>
        <!-- 自动建表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 打印SQL语句 -->
        <property name="show_sql">true</property>

        <!-- 映射 -->
        <mapping resource="com/wanshang/po/Users.hbm.xml" />

    </session-factory>

</hibernate-configuration>

报错时经常会指向这个方法,代码如下:

 /*通过字段集合查找*/
    public Users findByMap(Map<String, String> params) {
        // TODO
        Users users = new Users();
        //生成Session实例
        Session session = HibernateUtil.getSession();
        try{
            Criteria criteria = session.createCriteria(Users.class);
            //遍历Map
            Set<String> key = params.keySet();
            for(Iterator it = key.iterator();it.hasNext();){
                String k = (String) it.next(); //保存key
                criteria.add(Restrictions.eq(k, params.get(k)));//eq是等于,gt是大于,lt是小于,or是或
                System.out.println(k+":"+params.get(k));
            }
            users = (Users) criteria.list().get(0);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //关闭Session实例
            HibernateUtil.closeSession(session);
            return users;
        }
    }

下面是数据库截图:

图片说明

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 祈祷爱绝缘 2016-10-21 08:35
    已采纳

    问题应该是因为hibernate尝试在Users类中去找数据库表中的字段了,但是没看出来是哪块可能会出现这个错误。。

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2016-10-21 08:02

    数据库里有叫u_phone的字符串类型的字段么

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题