spring项目中,用到hibernate mapping。后台写完之后junit test可以跑通,数据库可以存取数据,但是页面数据传过来之后,hql报错。已经仔细检查过hql中是查询的是类名还是表名以及各项配置,hpl中查询的是map过的类名。单元测试跑得通,所以xml配置应该也没错。但是页面存取数据的时候会报错。仔细检查过hql的大小写,全半角以及空格是否为中文空格,依然不能解决问题。希望各路高人给指条明路,感激不尽。。
报错内容:[code=text]
Caused by: org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User where userName=?]
[/code]
具体的配置如下。
实体类:[code=java]
package com.huatek.cloud.usercenter.entity;
import com.huatek.framework.entity.FwAccount;
public class User extends FwAccount{
private static final long serialVersionUID = 6736542524764495567L;
private String seqNum; //数据的序列号,数据库中主键(id)。由于BaseServiceImpl中存在id字段,故重命名
private String userName; //用户名字段。如果注册方式为邮箱则它的值为email值;注册方式为手机则值为cellphone值
private String email; //用户的邮箱地址字段
private String cellphone; //用户的手机号字段
private int regType; //用户的注册类型字段
private String password; //用户的密码字段
private int activated; //用户的“是否激活”字段
private String validateCode; //用户的激活码字段
private java.sql.Timestamp registerTime; //用户的注册时间字段
//getter、setter方法略
}
[/code]
类与表的映射文件:User.hbm.xml
[code=text]
import 实体类所在的package;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<class name = "User" table="t_register">
<id name="seqNum" type="java.lang.String" column="id" length="100"></id>
<property name="userName" column="name" type="java.lang.String" length="30"></property>
<property name="email" column="email" type="java.lang.String" length="50"></property>
<property name="cellphone" column="phone" type="java.lang.String" length="11"></property>
<property name="regType" column="regType" type="java.lang.Integer" length="1"></property>
<property name="password" column="password" type="java.lang.String" length="50"></property>
<property name="activated" column="status" type="java.lang.Integer" length="1"></property>
<property name="validateCode" column="validateCode" type="java.lang.String" length="100"></property>
<property name="registerTime" column="registerTime" type="java.sql.Timestamp" length="100"></property>
</class>
[/code]
配置映射文件:application-base.xml
[code=text]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingLocations">
<list>
<!-- base frame -->
<value>classpath*:/com/huatek/framework/hibernate/maps/oracle/*.hbm.xml
</value>
<!-- data dictionary -->
<value>classpath*:/com/huatek/dictionary/hibernate/oracle/*.hbm.xml
</value>
<!-- data authority -->
<value>classpath*:/com/huatek/authority/hibernate/oracle/*.hbm.xml
</value>
<!-- cloud authority -->
<value>classpath*:/com/huatek/cloud/usercenter/hibernate/mysql/*.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.default_batch_fetch_size">30</prop>
</props>
</property>
<!-- <property name="lobHandler"> <ref local="oracleLobHandler" /> </property> -->
<property name="eventListeners">
<map>
<entry key="merge">
<bean
class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
[/code]
服务层中的实现方法,实际是就是判断userName是否已经存在库中以实现判断用户名重复:
[code=java]
@Override
public boolean cmpUserName(int regType, String userName) {
String hql = "from User where userName=?";
@SuppressWarnings("unchecked")
List<User> resultUser = hibernateTemplate.find(hql,userName); //每次debug单步跟进到这里报错
if (resultUser.isEmpty())
return userNameNotDup;
else
return userNameDup;
}
[/code]