使用spring + hibernate annotations的@OneToMany 一對多雙向關聯後出現異常:
java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
出現這種異常通常是由於Myeclipse的javaee.jar 版本較低,與Hibernate等內建的JPA出現了衝突。
網上一些處理方法是刪除項目裡的 刪除 建立web項目時內建的Javaee.jar,可是這樣帶來的後果是 httpServletRequest 等組件無法使用。
直接用JDK1.6建立工程
<prop key="javax.persistence.validation.mode">none</prop>
<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="javax.persistence.validation.mode">none</prop><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format">${hibernate.format}</prop><!-- 只要建立一個空的資料庫 DDL就能根據實體類自動產生資料庫表--><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop></props></property> <property name="packagesToScan"> <list> <value>com.gaozhi.school.**.model</value><!-- 掃描註解方式的實體類,也就是model --> </list> </property> </bean>