Hibernate报错 ** is not mapping

时间:2022-04-10 21:29:40

使用easyui+struts+hibernate 新增加一个页面功能时,总是报错,后来发现是数据库语句,不能写表名称,而是要写映射的数据库实体类名

1、struts文件修改增加action

<action name="UserAdd"
class="com.strawcom.actions.manage.UserMgnt"
method="execute">
<interceptor-ref name="checkLogin" />
<result name="input">/login.jsp</result>
<result name="success" type="json">
<param name="root">dataMap</param>
</result>
</action>

2、hibernate配置文件hibernate.cfg.xml

<mapping resource="com/strawcom/persistent/impl/hibernate/Carduser.hbm.xml" />

3、Carduser.hbm.xml 文件,映射实体类CharmRingUser

<hibernate-mapping>
<class name="com.strawcom.core.CharmRingUser" table="carduser" catalog="smartring">
<id name="phoneno" type="java.lang.String">
<column name="phoneno" length="20" />
<generator class="assigned" />
</id>
<property name="cardno" type="java.lang.String">
<column name="cardno" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" />
</property>
</class>
</hibernate-mapping>

4、数据库查询语句

虽然表名是carduser,但是,在写查询语句时,需要使用映射的实体类的名称。 HQL语句中表名应该是ORM映射的类名

public static List<CharmRingUser> getUserInfoFromDB(String phone, PageInfo pageInfo) {
String hql = "";
if ("".equals(phone) || null == phone || "null".equals(phone)) {
hql = "from CharmRingUser ";
} else {
hql = "from CharmRingUser as info where info.phoneno = " + phone;
}
return DataRepositoryFactory.getDataRepository().getPage(hql, pageInfo);
}

参考:http://blog.csdn.net/jsj_126abc/article/details/6582074