论坛首页 Java企业应用论坛

Hibernate 错误总结

浏览 4993 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (1) :: 隐藏帖 (1)
作者 正文
   发表时间:2010-08-18  

在我使用Hibernate的过程中一直遇到一些Exception,有些Exception遇到过多次,所以打算把这些异常记录下来;

1. Hibernate不能够获取当前的Session,获取时抛出异常如下

Exception in thread "main" org.hibernate.HibernateException: No session currently bound to execution context
	at org.hibernate.context.ManagedSessionContext.currentSession(ManagedSessionContext.java:74)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:700)
	at com.tibco.hibernate.coreAPI.ExceptionTest.main(ExceptionTest.java:11)

 

部分Hibernate.cfg.xml配置如下

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <property name="hbm2ddl.auto">update</property>

        <mapping class="com.tibco.hibernate.coreAPI.Student"/>

 

等效代码:

SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();

 

当我运行上面两行代码是就抛出上述异常;

解决办法:修给配置文件

<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>

 

替换成

<property name="current_session_context_class">thread</property>

 

原因是Hibernate版本更新造成,Hibernate3.2之前current_session_context_class 为org.hibernate.context.ManagedSessionContext, 而之后为thread,修改后就不会抛出异常

 

2. org.hibernate.exception.SQLGrammarException

异常如下:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.xxx.hibernate.many2one.uni.xml.Group]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)

 原因com.xxx.hibernate.many2one.uni.xml.Group中group与Mysql中关键字冲突,

解决办法,把表名重新命名就可以

<class name="com.xxx.hibernate.many2one.uni.xml.Group" dynamic-update="true" table="t_group">

 

 

   发表时间:2010-08-19  
3. HIbernate get时出错,错误如下:
Exception in thread "main" org.hibernate.InstantiationException: No default constructor for entity: com.xxx.hibernate.Group
	at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:107)
	at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:123)
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:606)
	at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3888)
	at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1412)
	at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1401)
	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1342)
	at org.hibernate.loader.Loader.getRow(Loader.java:1251)
	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:619)
	at org.hibernate.loader.Loader.doQuery(Loader.java:745)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
	at org.hibernate.loader.Loader.loadEntity(Loader.java:1933)

原因是:我在POJO类Group中写了构造方法,所以当get方法得到persistent状态的Group时,不能够生成对象,
修改方法:
将POJO类中的构造方法去掉即可
1 请登录后投票
   发表时间:2010-08-20  
有总结,不错.
0 请登录后投票
   发表时间:2010-08-20  
LZ hql语句中的Calendar类型的字段怎么比较大小啊!
0 请登录后投票
   发表时间:2010-11-04  
不錯,lz,我收藏到我的文章裏,不介意吧?哈哈
0 请登录后投票
   发表时间:2010-11-04  
不错,都是此经典的错误..
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics