Hibernate 中出现 xxx表 is not mapped xxx的问题
遇到这样的问题基本就是小白了,说的我自己,哈哈。根本原因是hql语法里面是POJO对象不是table。看下面的案例就知道了。
案例
- 配置文件
<hibernate-mapping>
<class name="com.entity.Userinfo" table="userinfo" catalog="netshop">
<id name="userId" type="java.lang.Integer">
<column name="userId" />
<generator class="native"></generator>
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" not-null="true" />
</property>
<set name="orders" inverse="true">
<key>
<column name="userId" not-null="true" />
</key>
<one-to-many class="com.entity.Order" />
</set>
</class>
</hibernate-mapping>
- 代码如下
public Userinfo search(Userinfo userinfo){
int result=0;
Session session=HibernateSessionFactory.getSession();
String hql="userinfo as u where u.username=? and u.password=?";
@SuppressWarnings("rawtypes")
Query query=session.createQuery(hql);
query.setString(0,userinfo.getUsername());
query.setString(1,userinfo.getPassword());
List list=query.list();
Iterator it=list.iterator();
if(it.hasNext()){
userinfo=(Userinfo)it.next();
result=1;
}
return userinfo;
}
- 修改如下
String hql="com.entity.Userinfo as u where u.username=? and u.password=?"; //写成 Userinfo 也可以
特记于此!以备勿忘!