我需要刪除一個實體,創建另一個:JPA:EclipseLink的不尊重em.remove()
@Stateless
public class StatelessBean {
@PersistenceUnit(unitName = "Unit001")
EntityManagerFactory emf;
protected void test() {
EntityManager em = emf.createEntityManager();
MyObj obj1 = em.find(MyObj.class, 100);
MyObj obj2 = new MyObj();
obj2.setKey("the same unique key as in obj1");
em.remove(obj1);
// em.flush();
em.persist(obj2); // works fine when flush() is uncommented
em.close();
}
}
如果我離開em.flush()
評論,然後我得到com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
(新舊對象具有相同的鍵值)
什麼可能是這種異常行爲的原因?
服務器:Glassfish的3.1.2
Eclipse持久服務 - 2.3.2.v20111125-r10461
的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Unit001">
<jta-data-source>jdbc/Unit001DS</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.target-database" value="MySQL"/>
</properties>
</persistence-unit>
</persistence>
連接池:
${ASADMIN} --port ${DOMAIN_ADMIN_PORT} create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.ConnectionPoolDataSource --property "User=user:Password=pass:URL=jdbc\:mysql\://${DB_ADDRESS}/db" Unit001DS
${ASADMIN} --port ${DOMAIN_ADMIN_PORT} create-jdbc-resource --connectionpoolid Unit001DS jdbc/Unit001DS
謝謝,但'其他JPA實現以同樣的方式執行相同的操作對我來說有點令人質疑 - 同樣的項目非常好地運行'Hibernate'作爲JPA提供程序。 – Osw 2012-04-12 09:24:58
您可能是對的,我不確定,只需在另一篇文章中閱讀即可。將編輯我的答案。 – 2012-04-12 09:29:17
是否有任何配置選項強制eclipselink生成sql作爲java代碼流?我很低調,希望能夠兼容hibernate的解決方案,而無需調整sql-deletes。 – Osw 2012-04-12 09:35:55