hibernate的cfg文件如下:
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.url">
jdbc:sqlserver://172.16.4.252:1433;databasename=CemsCenter
<!--jdbc:MySQL://172.16.4.187:3306/overtime-->
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123</property>
<!-- <property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>-->
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
<!--com.mysql.jdbc.Driver-->
</property>
<!-- C3P0连接池设定-->
<!-- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">120</property>
<property name="hibernate.c3p0.acquire_increment">2</property> -->
<property name="myeclipse.connection.profile">
sqlServer
</property>
<property name="show_sql">true</property>
<mapping resource="testDao/BasUserInfo.hbm.xml" />
</session-factory>hibernate的hbm文件如下:
<hibernate-mapping>
<class name="testDao.BasUserInfo" table="Bas_UserInfo" schema="dbo" >
<id name="id" type="java.lang.String">
<column name="ID" length="36" />
<generator class="native" />
</id>
<property name="account" type="java.lang.String">
<column name="Account" />
</property>
<property name="userName" type="java.lang.String">
<column name="UserName" />
</property>
<property name="pwd" type="java.lang.String">
<column name="PWD" />
</property>
<property name="roleId" type="java.lang.String">
<column name="RoleID" />
</property>
<property name="email" type="java.lang.String">
<column name="EMail" />
</property>
<property name="del" type="java.lang.Short">
<column name="Del" />
</property>
</class>
</hibernate-mapping>java类如下:
public class BasUserInfo implements java.io.Serializable
{ // Fields private String id;
private String account;
private String userName;
private String pwd;
private String roleId;
private String email;
private Short del; // Constructors /** default constructor */
public BasUserInfo()
{
} /** minimal constructor */
public BasUserInfo(String id, String account, String pwd)
{
this.id = id;
this.account = account;
this.pwd = pwd;
} /** full constructor */
public BasUserInfo(String id, String account, String userName, String pwd, String roleId, String email, Short del)
{
this.id = id;
this.account = account;
this.userName = userName;
this.pwd = pwd;
this.roleId = roleId;
this.email = email;
this.del = del;
} // Property accessors public String getId()
{
return this.id;
} public void setId(String id)
{
this.id = id;
} public String getAccount()
{
return this.account;
} public void setAccount(String account)
{
this.account = account;
} public String getUserName()
{
return this.userName;
} public void setUserName(String userName)
{
this.userName = userName;
} public String getPwd()
{
return this.pwd;
} public void setPwd(String pwd)
{
this.pwd = pwd;
} public String getRoleId()
{
return this.roleId;
} public void setRoleId(String roleId)
{
this.roleId = roleId;
} public String getEmail()
{
return this.email;
} public void setEmail(String email)
{
this.email = email;
} public Short getDel()
{
return this.del;
} public void setDel(Short del)
{
this.del = del;
}
}
错误代码如下:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: BasUserInfo is not mapped [from BasUserInfo]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.sdl.dao.HisData.test.main(test.java:47)各位大侠们 急啊 这问题太奇怪了 我明明映射了hbm文件啊 它怎么会报这个错误呢?
private String id;
private String account;
private String userName;Id 没指定主键生成方式 没标明 id为主键 : 如@Id @GeneratorValue(...) ; 实体类 上面 没加上 @Entity
可能你的BasUserInfo.hbm.xml放错地方了。
这句话是关键,如果你是用的hql的话,在你的sql语句应该写Statement stmt=stmtFactory.createStatement("from BasUserInfo","where");
from后面接的是实体类名,还有BasUserInfo.hbm.xml,BasUserInfo.java这两个配置文件最好放在一起,在实体类那个包里
非常赞同,明显是 执行hql时候出现了问题,应该是 hql语句的问题
都报了BasUserInfo is not mapped竟然还说是hql有问题....再看看最初抛出的at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)。
requireClassPersister 就是缺少持久化类啊 可能是BasUserInfo.hbm.xml,BasUserInfo.java没放一起吧