1. 程式人生 > >hibernate框架常見異常集錦

hibernate框架常見異常集錦

一.entity找不到問題一般出現在hibernate.cfg.xml檔案中

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.lrq.entity.Developer
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1479)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at com.lrq.test.TestDevAndPro.main(TestDevAndPro.java:42)


解決方案:在hibernate.cfg.xml配置相關mapper對映


<!--指明表和實體類的對映-->
    <mapping resource="com/lrq/entity/Developer.hbm.xml"/>
    <mapping class="com.lrq.entity.Developer"/>


    <mapping resource="com/lrq/entity/Project.hbm.xml"/>

    <mapping class="com.lrq.entity.Project"/>

二.主鍵自增沒設定
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.lrq.entity.Project


解決方案:在Project.hbm.xml裡面配置
<class name="com.lrq.entity.Project" table="project" schema="test">
        <id name="proId">
            <column name="pro_id" sql-type="int(11)"/>
            <generator class="identity"/>
        </id>

三.Transation通過get方式得到而不是begin,會顯示沒有開啟事務
       Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started

    解決方案:
   Transaction tx=session.beginTransaction();

四.沒有註冊使用者名稱和密碼錯誤
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.lrq.test.TestOneToOne.main(TestOneToOne.java:15)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at com.lrq.util.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:15)
... 1 more
Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)


解決方案:
<property name="connection.username">root</property>
<property name="connection.password">12345</property>

五.Xml設定關聯關係錯誤
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.lrq.test.TestOneToOne.main(TestOneToOne.java:15)
Caused by: org.hibernate.MappingException: property-ref [user] not found on entity [com.lrq.entity.Weapon]
at org.hibernate.mapping.PersistentClass.getReferencedProperty(PersistentClass.java:385)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1724)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at com.lrq.util.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:15)
... 1 more
Caused by: org.hibernate.MappingException: property [user] not found on entity [com.lrq.entity.Weapon]
at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:442)
at org.hibernate.mapping.PersistentClass.getReferencedProperty(PersistentClass.java:382)
... 6 more


解決方案:
<one-to-one name="weapon" class="com.lrq.entity.Weapon" property-ref="hero"/>

六.屬性Id沒有預設值,一般是表裡面沒設定自增
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
aorg.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Caused by: java.sql.SQLException: Field 'weaponId' doesn't have a default value