You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by areider <op...@reider.net> on 2010/11/04 11:41:50 UTC
Order of insert
I have a model, which for purposes of my question, includes a structure like
this:
class Lot
@OneToMany(mappedby="lot",cascade =
{CascadeType.MERGE,CascadeType.PERSIST}))
List<Car> carList;
@OneToMany(mappedby="lot",cascade =
{CascadeType.MERGE,CascadeType.PERSIST}))
List<SalesPerson> salesPersonList;
class Car
@Column
String lotFK;
@Column
String salesPersonFK;
@ManyToOne
Lot lot;
@ManyToOne
SalesPerson salesPerson;
class SalesPerson
@ManyToOne
Lot lot;
@OneToMany(mappedBy="salesPerson"cascade =
{CascadeType.MERGE,CascadeType.PERSIST}))
List<Car> carList;
So cars are owned by lots and salesPersons and has non-nullable FK's to
both, with RI.
When persisting a lot such as:
lot1
// carList
--ford
// salesPersonList
--john
----ford
it seems JPA just does a top down traversal of the graph for the insert
order. Ie ford is inserted before john, which violates the RI for a Car.
I tried a few things:
- <property name="openjpa.jdbc.UpdateManager" value="operation-order"/> in
persistence.xml
- annotating the foreign keys with @ForeignKey (then openJpa excludes the
column from insert, which then fails because the field is non-nullable)
- removing the mapping Lot:car; ( works but I need the mapping).
- defining Lot::salesPersonList ahead of Lot::carList.
- NOT cascading persist from Lot to carList.
- in @PrePersist of Lot, clear the carList, and JPA saves them later in the
carLists of salesPersons. A real hack, but happens to work because in the
real model, the graph will always be complete with respect to cars and
salesPersons.
Any other options for inducing the correct order or the inserts?
This is in openJPA version that comes with IBM RAD7.5.5, which appears to
be 1.2.3.
--
View this message in context: http://openjpa.208410.n2.nabble.com/Order-of-insert-tp5704835p5704835.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Order of insert
Posted by areider <op...@reider.net>.
Thanks Mike,
I tried your suggestion, but it had no effect. The persist-order (as shown
by logging @postPersist) is still in depth-first order of the object graph.
I'm almost certain I tried @ForeignKey before and after adding the
UpdateManager property.
Using @ForeignKey had the curious effect of causing the column to be omitted
from the insert (and DB2 apparently validates non-nullable constraints
before referential integrity constraints, and so the insert fails on
null-nullable column is NULL).
--
View this message in context: http://openjpa.208410.n2.nabble.com/Order-of-insert-tp5704835p5707205.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Order of insert
Posted by Michael Dick <mi...@gmail.com>.
You can try having OpenJPA inspect the tables for foreign keys at runtime.
Just add this to persistence.xml :
<property name="openjpa.jdbc.SchemaFactory" value="native"/>
This should behave identically to using the @ForeignKey annotations though.
When you tried that did you use the operation-order update manager, or use
the default (batching-constraint)? Either the batching-constraint or
constraint update manager should handle most FK dependencies.
-mike
On Thu, Nov 4, 2010 at 5:41 AM, areider <op...@reider.net> wrote:
>
> I have a model, which for purposes of my question, includes a structure
> like
> this:
>
> class Lot
> @OneToMany(mappedby="lot",cascade =
> {CascadeType.MERGE,CascadeType.PERSIST}))
> List<Car> carList;
> @OneToMany(mappedby="lot",cascade =
> {CascadeType.MERGE,CascadeType.PERSIST}))
> List<SalesPerson> salesPersonList;
> class Car
> @Column
> String lotFK;
> @Column
> String salesPersonFK;
> @ManyToOne
> Lot lot;
> @ManyToOne
> SalesPerson salesPerson;
> class SalesPerson
> @ManyToOne
> Lot lot;
> @OneToMany(mappedBy="salesPerson"cascade =
> {CascadeType.MERGE,CascadeType.PERSIST}))
> List<Car> carList;
>
> So cars are owned by lots and salesPersons and has non-nullable FK's to
> both, with RI.
>
> When persisting a lot such as:
>
> lot1
> // carList
> --ford
> // salesPersonList
> --john
> ----ford
>
> it seems JPA just does a top down traversal of the graph for the insert
> order. Ie ford is inserted before john, which violates the RI for a Car.
>
> I tried a few things:
> - <property name="openjpa.jdbc.UpdateManager" value="operation-order"/> in
> persistence.xml
> - annotating the foreign keys with @ForeignKey (then openJpa excludes the
> column from insert, which then fails because the field is non-nullable)
> - removing the mapping Lot:car; ( works but I need the mapping).
> - defining Lot::salesPersonList ahead of Lot::carList.
> - NOT cascading persist from Lot to carList.
> - in @PrePersist of Lot, clear the carList, and JPA saves them later in the
> carLists of salesPersons. A real hack, but happens to work because in the
> real model, the graph will always be complete with respect to cars and
> salesPersons.
>
> Any other options for inducing the correct order or the inserts?
>
>
> This is in openJPA version that comes with IBM RAD7.5.5, which appears to
> be 1.2.3.
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Order-of-insert-tp5704835p5704835.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>