2013-05-20 16 views
2

我被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> 

謝謝你的未來在你的答案,並聆聽任何附加信息。

+0

您可以顯示OutilsJpaController.create的實現以及實體Outils的源代碼嗎? – Eelke

+0

由於擔心不混亂,我給他們在執行藍色鏈接。 –

+0

沒有看到任何錯誤,您是否在數據庫中檢查對象是否實際上保留在數據庫中,而EclipseLink是否沒有填充該ID?對於IDENTITY,這些值由DB生成,因此可能EclipseLink不會讀取id。 – Eelke

回答

0

(不是一個真正的答案,但我不能評論顯示此) 增加日誌您的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> 
      <property name="eclipselink.logging.level" value="FINE"/> 
      <property name="eclipselink.logging.level.sql" value="FINE"/> 
      <property name="eclipselink.logging.parameters" value="true"/> 
     </properties> 
    </persistence-unit> 
</persistence> 
0

您需要提交或任何東西之前刷新事務將寫入數據庫。如果您使用的是IDENTITY排序(我強烈建議不要使用IDENTITY,如果db支持,使用TABLE或SEQUENCE),那麼在插入發生之前不能分配Id,因此persist()不會分配Id TABLE和SEQUENCE)。

你的create()方法做了什麼?你的Id如何映射?

+0

1-'utx.begin();'2- 'em = getEntityManager();'3-'em.persist(outils);'4-'utx.commit();' –

相關問題