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 <[email protected]> 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.
>

Reply via email to