Hibernate常見異常(整理ing)

 

Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

不注意的話,還真的有點無所適從,Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1這個異常是由於主鍵設置爲自增長,而在我們插入記錄的時候設置了ID的值導致的。

java.lang.NoClassDefFoundError: antlr/ANTLRException


開始沒有注意到這個,只看下面的詳細情況.結果調試了兩天還是不行.沒辦法,搜索了一下antlr/ANTLRException,結果發現是少了antlr-2.7.5H3.jar包

 

使用Hibernate3.2 以上版本的註解映射,需導入以下jar包: 織夢好,好織夢
1. ejb3-persistence.jar
2. hibernate-annotations.jar
3. hibernate-commons-annotations.jar
4. hibernate-core.jar
如果和Spring 整合,那麼需要另外導入一個jar包(spring.jar)。
注意:
此spring.jar必須是 Spring2.5以上版本的,因爲,Spring2.5之前org.springframework.orm.hibernate3.LocalSessionFactoryBean類中,並沒有 packageToScan 這個屬性,只有mappingResuorces這個屬性。而packageToScan這個屬性正是映射包中的類,而mappingResuorces只是映射某個文件。

com.test.po
這樣,即可完成映射。

在使用 Hiberante註解映射和Spring整合過程中可能遇到的異常:
如出現java.lang.NoClassDefFoundError: org/dom4j/ ,導入dom4j.jar包即可
如出現java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory , 需要導入以下四個jar包(1: slf4j-api-1.4.3.jar、2:slf4j-jdk14-1.4.3.jar、3:slf4j-log4j12-1.4.3.jar、4:log4j-over-slf4j-1.4.3.jar) 即可
如出現java.lang.NoClassDefFoundError: javax/transaction/TransactionManager , 需要導入 jta.jar包
java.lang.ClassNotFoundException: antlr.ANTLRException 則需要導入ant.jar包
org.springframework.util.Assert.noNullElements([Ljava/lang/Object;Ljava/lang/String;)V Invalid property 'packagesToScan' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 這是由於,spring.jar包的版本不符,把spring.jar換成Spring 2.5以上版本即可
6. java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V jar包衝突,刪除asm-2.2.3.jar 即可

 

ids for this class must be manually assigned before calling save()

oracle:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "ID", unique = true, nullable = false, length = 50)
public String getId() {
    return this.id;
}

id最好是自動生成:
如果是配置Hbm文件,可以加入<generator class="assigned"></generator>
如果是註解:@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}


 hibernate   +MySQL 5中,出現錯誤提示:
Field 'id' doesn't have a default value
解決方法
打開my.ini(my-template.ini),查找
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
修改爲
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Spring與Hibernate整合getHibernateTemplate().delete(object)刪除不了數據


Spring 還是沒有封裝 Hibernate 的刪除、插入時事物的開啓 和提交..
找了資料我去hibernate.cfg.xml 添加了一句
<property name="connection.autocommit">true</property>

 

Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition 錯誤解決

錯誤代碼:
  org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
錯誤原因:
  OpenSessionInViewFilter在getSession的時候,會把獲取回來的session的flush mode 設爲FlushMode.NEVER。然後把該sessionFactory綁定到TransactionSynchronizationManager,使request的整個過程都使用同一個session,在請求過後再接除該sessionFactory的綁定,最後closeSessionIfNecessary根據該session是否已和transaction綁定來決定是否關閉session。在這個過程中,若HibernateTemplate 發現自當前session有不是readOnly的transaction,就會獲取到FlushMode.AUTO Session,使方法擁有寫權限。
也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉爲Flush.AUTO,擁有insert,update,delete操作權限,如果沒有transaction,並且沒有另外人爲地設flush model的話,則doFilter的整個過程都是Flush.NEVER。所以受transaction保護的方法有寫權限,沒受保護的則沒有。
參考文章:
  http://calvin.blog.javascud.org/post/46.htm
解決辦法:
  採用spring的事務聲明,使方法受transaction控制
<bean id="baseTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
          abstract="true">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="proxyTargetClass" value="true"/>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="add*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean> 
    <bean id="userService" parent="baseTransaction">
        <property name="target">
            <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
        </property>
    </bean>


 

發佈了21 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章