2

It'll be weeks that I'm stuck with EclipseLink. I can not persist an object in my database. I use netbeans 7.3. I encountered this problem when I started designing a web application. What follows is the approach I have adopted. It may be that I do without me realize a mistake.

After that netbeans has finished generating the project files I configured the jndi. Then I converted automatically with netbeans, the database tables in entity object. here is the link

then, from these classes, I created their JPAController . (Always automatically with netbeans)

and finally, as a test, I just instantiate the description of "Outils" and leave the fields empty id. Since the latter automatically increment in the database, if the persistence is done well, I should have an id when I appear with out the console.

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

and I get as result: Outils[ id=null ].

I have no error or on glassfish even less about the debugger.

Ps : Here are the persistence.xml file

<?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>

Thank you for your future is in your answers and listening for any additional information.

4

2 回答 2

0

在将任何内容写入数据库之前,您需要提交或刷新事务。如果您正在使用 IDENTITY 排序(我强烈建议不要使用 IDENTITY,如果 db 支持,请使用 TABLE 或 SEQUENCE),那么在插入发生之前无法分配 Id,因此 persist() 不会分配 Id(因为它为表和序列)。

你的 create() 方法是做什么的?您的 ID 是如何映射的?

于 2013-05-21T15:18:25.047 回答
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>
于 2013-05-20T11:59:56.850 回答