Quantcast

JPA insertion order

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JPA insertion order

Adme Admirolas
Hello,

I'm facing problems, when I persist JPA entities. Here is scenario:

I'm using CDI, and inject EntityManager and UserTransaction into named bean. I have entity, that we can call MainEntity. It has 2 "@ManyToOne(optional = false, fetch = LAZY, cascade = { PERSIST, REFRESH })" relationship to other entities, lets call them MinorEntity. So, when i commit UserTransaction, the JPA first of all inserts Main, then MinorEntity, and then try to update MainEntity table again. The problem is, that i have not null constraint on database.
I googled a little bit, and found that i should use OpenJPA @ForeignKey annotation, to force OpenJPA first of all insert MinorEntity. But this didn't helped to me, the insert order is still incorrect.

The strangest thing, is that i have unit test, that tries to simulate this situation using Resource Local transaction type. And it does not fails.

What I'm doing wrong? Is there need, to do some additional Geronimo app server configuration?

Here is persistence.xml of container managed persistence context:

<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="PRNUBEMJPA" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>jdbc/PRNUBEM</jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
            <property name="openjpa.jdbc.Schema" value="PRNUBEM"/>
            <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="printParameters=true"/>
            <property name="openjpa.InverseManager" value="true"/>
        </properties>
    </persistence-unit>
</persistence>


And here is persistence.xml of resource local persistence context:

<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="PRNUBEMJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
            <property name="openjpa.ConnectionURL"
                value="jdbc:db2://localhost:50000/PRNUBEM" />
            <property name="openjpa.ConnectionDriverName"
                value="com.ibm.db2.jcc.DB2Driver" />
            <property name="openjpa.ConnectionUserName" value="****" />
            <property name="openjpa.ConnectionPassword" value="***" />
            <property name="openjpa.jdbc.Schema" value="PRNUBEM"/>
            <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="printParameters=true"/>
            <property name="openjpa.InverseManager" value="true"/>
        </properties>
        
    </persistence-unit>
</persistence>


Please help, because if I will not be able to solve this soon, I will be forced to move to some another JPA implementation, or even another APP server.

Admirolas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JPA insertion order

Ivan Xu
I am thinking that this is not Geronimo specific issue, forward your email to openjpa user mail list, there are many experts there :-)

2012/3/26 Adme Admirolas <[hidden email]>
Hello,

I'm facing problems, when I persist JPA entities. Here is scenario:

I'm using CDI, and inject EntityManager and UserTransaction into named bean. I have entity, that we can call MainEntity. It has 2 "@ManyToOne(optional = false, fetch = LAZY, cascade = { PERSIST, REFRESH })" relationship to other entities, lets call them MinorEntity. So, when i commit UserTransaction, the JPA first of all inserts Main, then MinorEntity, and then try to update MainEntity table again. The problem is, that i have not null constraint on database.
I googled a little bit, and found that i should use OpenJPA @ForeignKey annotation, to force OpenJPA first of all insert MinorEntity. But this didn't helped to me, the insert order is still incorrect.

The strangest thing, is that i have unit test, that tries to simulate this situation using Resource Local transaction type. And it does not fails.

What I'm doing wrong? Is there need, to do some additional Geronimo app server configuration?

Here is persistence.xml of container managed persistence context:

<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="PRNUBEMJPA" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>jdbc/PRNUBEM</jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
            <property name="openjpa.jdbc.Schema" value="PRNUBEM"/>
            <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="printParameters=true"/>
            <property name="openjpa.InverseManager" value="true"/>
        </properties>
    </persistence-unit>
</persistence>


And here is persistence.xml of resource local persistence context:

<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="PRNUBEMJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
            <property name="openjpa.ConnectionURL"
                value="jdbc:db2://localhost:50000/PRNUBEM" />
            <property name="openjpa.ConnectionDriverName"
                value="com.ibm.db2.jcc.DB2Driver" />
            <property name="openjpa.ConnectionUserName" value="****" />
            <property name="openjpa.ConnectionPassword" value="***" />
            <property name="openjpa.jdbc.Schema" value="PRNUBEM"/>
            <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="printParameters=true"/>
            <property name="openjpa.InverseManager" value="true"/>
        </properties>
        
    </persistence-unit>
</persistence>


Please help, because if I will not be able to solve this soon, I will be forced to move to some another JPA implementation, or even another APP server.

Admirolas



--
Ivan
Loading...