2 chn wwz chn_wwz 于 2016.05.05 23:46 提问

Hibernate 的 getSessionFactory();方法 5C

在下自学Hibernate方法,有错误之处请各位多多指点
废话少说直接上代码
DAOImpl 中的代码:

public class UserDAOImpl extends HibernateTemplate implements UserDAO {

private SessionFactory factory;

@Override
public User findUserByName(String name) {
    System.out.println("进来了DAO层");
    try{
        //修改开始处
        Configuration conf = new Configuration();
        conf = conf.configure();
        factory = conf.buildSessionFactory();
        //修改结束处
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
    Session session = factory.openSession();
    Transaction tx = null;
    List<User> users = new ArrayList<User>();
      try{
         tx = session.beginTransaction();
         String hql = "FROM User U WHERE U.name = :name";
         Query query = session.createQuery(hql);
         query.setParameter("name",name);
         users = (List<User>)query.list();
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
    return users.get(0);
}

public void setFactory(SessionFactory factory) {
    this.factory = factory;
}

}

dao.xml 中的配置:

 <bean id="userDAO" class="com.ssh.dao.UserDAOImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

这种情况下,系统是没有错的.只不过查询的结果非常慢

所以我准备换成用Spring来管理hibernate的数据库懒加载

为了大家看的明白 我就只修改了
//修改开始处 和//修改结束处 中间的代码

替换成了:

factory = this.getHibernateTemplate().getSessionFactory();

就这样,系统就报错了

贴上错误提示

 org.hibernate.hql.ast.QuerySyntaxException: User is not mapped. [FROM User U WHERE U.name = :name]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:157)
    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:265)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
    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:218)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
    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:1583)
    at com.ssh.dao.UserDAOImpl.findUserByName(UserDAOImpl.java:44)
    at com.ssh.service.UserServiceImpl.getUserByUserName(UserServiceImpl.java:14)
    at com.ssh.action.loginAction.login(loginAction.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:745)
Caused by:  User is not mapped.
    ... 87 more

提示 User is not mapped (我是知道hql中的User 并不是数据库中的表名,所以这里不会错的)
那么为什么会提示User没有在数据库中映射????

所以请问各位大神这是错在哪了.有需要贴别的地方的代码请尽管说.
我把我所有的币都献上了

应要求 贴上user.hbm.xml的代码:

<hibernate-mapping>
   <class name="com.ssh.entity.User" table="user">
      <id name="id" type="int" column="id">
      </id>
      <property name="name" column="name" type="string"/>
      <property name="password" column="password" type="string"/>
   </class>
</hibernate-mapping> 

7个回答

CSDNXIAON
CSDNXIAON   2016.05.05 23:51

Hibernate Session的delete方法
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

devmiao
devmiao   Ds   Rxr 2016.05.05 23:56
chn_wwz
chn_wwz 回复苏小喵: 不太明白,请问我要怎么改
大约 2 年之前 回复
xxj19931101
xxj19931101   2016.05.06 00:02

首先:在使用this.getHibernateTemplate() 时,你的类要继承HibernateSupport类,然后貌似只有hibernate3中可以这样用,所以要看你hibernate的版本是多少

chn_wwz
chn_wwz 回复xxj19931101: 我继承的那个类已经继承了HibernateSupport了 不过你说的版本问题我倒是要看看
大约 2 年之前 回复
xxj19931101
xxj19931101   2016.05.06 00:05

查看一下你的User.hbm.xml里面是否正确

chn_wwz
chn_wwz 已经贴上 user.hbn.xml代码
大约 2 年之前 回复
Royal_lr
Royal_lr   Ds   Rxr 2016.05.06 10:02

修改前,,报这个错误了吗?

Royal_lr
Royal_lr   Ds   Rxr 2016.05.06 10:04

看到了,,没报错,,你可以直接用hibernateTemplate的find方法查询

apbbbbb
apbbbbb   2016.05.06 11:18

你把User换成user试试,因为你的xml文件中用的user或者把xml中的user换成User 然后重启,hql语言用的是实体类,但你的实体类没有贴出来,不知道你怎么写的,是否用的注解?你先试试吧

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
getHibernateTemplate()与getSession()的区别
1. getHibernateTemplate已经封装好了一些基本的方法,可以直接去用 而getSession只是获取一个数据工厂的session,然后大部分方法都需要自己写,加hql语句,然后用query方法执行   2. HibernateTemplate 提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,Spring 2.0更
Hibernate中session.connection()的替代方法
Hibernate中session.connection()的替代方法      Hibernate3.3.2版本中getSession().connection()已被弃用,替代方法SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection() 来自类org.springframework.orm.hibern
hibernate获取sessionFactory工具类
hibernate 封装小工具,可以存入工具包,调用其方法可以在持久层获取sessionFactory和session
hibernate之SessionFactory和Session
1. 关于SessionFactory 1.) SessionFactory是一个与连接池的类差不多的东西,在这里存着多个Session—与数据库的会话(相当于connection) 2.) 因为连接数据库的配置信息和映射信息都在SessionFactory,比较占内存,所以在hibernate中SessionFactory只有一个 3.) SessionFactory是线程安全的
etHibernateTemplate().getSessionFactory().getCurrentSession()事务中的session
在同一个事务里,getHibernateTemplate().getSessionFactory().getCurrentSession()
this.getsession和this.getHibernateTemplate().getSessionFactory().getCurrentSession().OpenSession区别
这个是hibernate与spring整合开发,让DAO继承了spring的HibernateDaoSupport,这样的确能够提高开发效率 ,但是不够灵活,而且使DAO层依赖于spring的api,增加了耦合。但是不考虑复用的话还可以。     下面一个一个的分析: this.getsession实际上是调用了父类中的方法获得session。使用spring管理hibernate的Sessio
hibernate中的SessionFactory的openSession与getCurrentSession
hibernate在得到session的方法中,openSession与getCurrentSession方法的区别? Configuration cfg = new AnnotationConfiguration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session
hibernateTemplate.getSessionFactory().getCurrentSession()
hibernateTemplate.getSessionFactory().getCurrentSession() 我们使用spring和hibernate结合,操作数据库最常用可能是HibernateTemplate,HibernateTemplate中集成了很多使用的方法,可惜的是没的createQuery方法,也许我们使用hibernate的时候喜欢使用Query,我们可能会封装hib
Hibernate 5.2.x 中 sessionFactory 的获取方式
本文给出了最新版 Hibernate 初始化 session 的工具类辅助代码,并对比给出了 Hibernate 4.3 版的等效代码。
hibernate的Sessionfactory.getCurrentSession详解和在项目中常用方法
Sessionfactory.getCurrentSession与 openSession() 的区别 1. getCurrentSession创建的session会和绑定到当前线程,而openSession不会。 2. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭。 这里getCurrentSession本地事务(本