我被EclipseLink困住了幾個星期。我無法在數據庫中保留一個對象。我使用netbeans 7.3。我開始設計Web應用程序時遇到此問題。接下來是我採用的方法。這可能是因爲我沒有意識到一個錯誤。EclipseLink在Netbeans上不起作用,這是正常的嗎?
之後,netbeans已經完成生成項目文件我配置了jndi。然後我使用netbeans(實體對象中的數據庫表)自動轉換。 here is the link
然後,從這些類中,我創建了它們的JPAController。 (總是自動使用netbeans)
最後,作爲一個測試,我只是實例化「Outils」的描述並將這些字段留空。由於後者在數據庫中自動遞增,如果持久性很好,那麼在出現控制檯時我應該有一個id。
<body>
<h1>Hello World!</h1>
<%
Outils o = new Outils();
o.setDesignation("hammers");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test_EclipseLinkPU");
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
OutilsJpaController o_ctrl = new OutilsJpaController(utx, emf);
o_ctrl.create(o);
out.println("this is the id of hammer " + o.toString());
%>
</body>
我得到的結果是:Outils [id = null]。
我沒有錯誤或對glassfish甚至更少的調試器。
PS:這裏是persistence.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
謝謝你的未來在你的答案,並聆聽任何附加信息。
您可以顯示OutilsJpaController.create的實現以及實體Outils的源代碼嗎? – Eelke
由於擔心不混亂,我給他們在執行藍色鏈接。 –
沒有看到任何錯誤,您是否在數據庫中檢查對象是否實際上保留在數據庫中,而EclipseLink是否沒有填充該ID?對於IDENTITY,這些值由DB生成,因此可能EclipseLink不會讀取id。 – Eelke