We have an entity A having a OneToOne unidirectional relationship to entity B
configured like this:

In entity A:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "B_ID")
private B b;

Now we create new entities A and B and would like to persist them:

final A a = new A();
final B b = new B();
a.setB(b);

em.persist(a);

We get a PersistenceException because OpenJPA tries to insert the entity
with the foreign key first:

<openjpa-1.2.3-SNAPSHOT-r422266:1053401 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: ORA-02291: integrity
constraint violated - parent key not found 
 {prepstmnt 1708156368 
INSERT INTO A (ID, ROW_ERF_TSTAMP, ROW_ERF_USER,  ROW_MUT_VERSION, B_ID) 
    VALUES (?, ?, ?, ?, ?) 
[params=(long) 396, (Timestamp) 2011-11-09 15:38:12.048, (String) TEST,
(int) 1, (long) 2772]} [code=2291, state=23000]

Why does OpenJPA first try to insert entity A and not B? An how can we force
OpenJPA to execute statements in the right order?

Interestingly this works on our OneToMany relationships.

In the internet I found  https://issues.apache.org/jira/browse/OPENJPA-1961
this  and  http://www-01.ibm.com/support/docview.wss?uid=swg1PK74266 this .
But these issues are for OneToMany relationships. IBM provides a solution
using the OpenJPA property openjpa.jdbc.SchemaFactory. But we would like to
understand why persisting does not work in the OneToOne case above. So has
somebody an explanation for our issue? Thank you in advance!

We use WebSphere server 7.0.0.19 and OpenJPA 1.2.3.

--
View this message in context: 
http://openjpa.208410.n2.nabble.com/Constraint-violation-using-OneToOne-relationship-tp6978223p6978223.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to