You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by femski <hy...@yahoo.com> on 2008/06/23 05:49:55 UTC
Foreign key violation while persisting - statement order issue.
Folks !
I am getting foreign key violation error persisting a simple hierarchy of
objects which looks like below:
class SearchTask {
collection of Search Log {
Collections of A
Collection of B
Collection of C
(A, B, C have one more layer of collections)
}
}
class FreeSearch derived from Search Task using a discriminator {
collection of Sources
}
Cascades are all set to ALL (on both sides of one-to-many relationships).
I expect it to persist SearchTask/FreeSearch first - generate its primary
key and substitute them in foreign key attributed of SearchLog and further
down. openJPA instead tries to insert A, B C etc before SearchLog. And
Sources before FreeSearch etc. As if it doesn't understand the hierarchy.
I am initializing collections in the constructors (using new ArrayList) and
populate them later. I have printed the graph to make sure everything is as
I would expect. Primary keys are generated by
OpenJPA correctly. It even sets foreign key values correctly. SQL logs shows
its a statement ordering issue.
When I completely dumb down the model and drop foreign key constraints then
it works - but only sometimes. Generated SQL log shows statement order is
random at best. Retry works sometimes. I haven't dropped all constraints and
tried persisting entire model. All my foreign keys are not null and I don't
want to change my design.
Even when I do manual persist walking down the tree in right order, inserts
are done in random order (even though I have read OpenJPA does not
reordering of its own) and fails because of foreign keys.
I am running OpenEJB 3.0 embedded inside Tomcat which no change to original
configuration. Database is Postgres 8.1.
What am I missing ?
Any help would be appreciated.
thank you,
-Sanjay Kumar
--
View this message in context: http://www.nabble.com/Foreign-key-violation-while-persisting---statement-order-issue.-tp18062156p18062156.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Foreign key violation while persisting - statement order issue.
Posted by femski <hy...@yahoo.com>.
Thank you so much. This seems to do the trick.
I had generated my classes using NetBeans and it seems to have used the the
foreign keys in the database to generate the relationship. So I was
expecting OpenJPA to figure out foreign keys from the generated class
hierarchy, relationships and cascade attributes.
It seems it takes native(ForeignKeys=true) for OpenJPA to read and
understand the foreign keys. Its extremely valuable so much so that I would
think this should be default behavior.
thanks again,
-Sanjay
techhusky wrote:
>
> Sanjay,
>
> If you have foreign keys that are not known to OpenJPA (which is more
> likely when you are mapping to a schema that was not created by the
> mapping tool), statements may not get ordered correctly. If this is
> the case, adding this property to your persistence.xml may correct the
> problem:
>
> <property name="openjpa.jdbc.SchemaFactory"
> value="native(ForeignKeys=true)"/>
>
> This will result in object mapping validation against the database
> schema, picking up additional foreign key constraints.
>
> -Jeremy
>
>
--
View this message in context: http://www.nabble.com/Foreign-key-violation-while-persisting---statement-order-issue.-tp18062156p18104004.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Foreign key violation while persisting - statement order issue.
Posted by Jeremy Bauer <te...@gmail.com>.
Sanjay,
If you have foreign keys that are not known to OpenJPA (which is more
likely when you are mapping to a schema that was not created by the
mapping tool), statements may not get ordered correctly. If this is
the case, adding this property to your persistence.xml may correct the
problem:
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
This will result in object mapping validation against the database
schema, picking up additional foreign key constraints.
-Jeremy
Re: Foreign key violation while persisting - statement order issue.
Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
> Primary keys are generated by OpenJPA correctly.
If the classes are using database-generated identity as primary key then
new instances are to be flushed (or use a database sequence with
pre-allocation) to get the identity. Or
X pc = new X();
em.persist(pc);
OpenJPAPersistence.cast(em).getObjectId(pc);
will assign the primary key eagerly.
>Even when I do manual persist walking down the tree in right order, inserts
are done in random order
Configure OpenJPA to honor ordering of your persistent operations
<property name="openjpa.jdbc.UpdateManager" value="operation-order"/>
Or, if you want batching
<property name="openjpa.jdbc.UpdateManager"
value="batching-operation-order"/>
Note that this updating scheme is non-default and hence has to be set
explicitly.
femski wrote:
>
> Folks !
>
> I am getting foreign key violation error persisting a simple hierarchy of
> objects which looks like below:
>
> class SearchTask {
> collection of Search Log {
> Collections of A
> Collection of B
> Collection of C
> (A, B, C have one more layer of collections)
> }
> }
>
> class FreeSearch derived from Search Task using a discriminator {
> collection of Sources
> }
>
> Cascades are all set to ALL (on both sides of one-to-many relationships).
>
> I expect it to persist SearchTask/FreeSearch first - generate its primary
> key and substitute them in foreign key attributed of SearchLog and further
> down. openJPA instead tries to insert A, B C etc before SearchLog. And
> Sources before FreeSearch etc. As if it doesn't understand the hierarchy.
>
> I am initializing collections in the constructors (using new ArrayList)
> and populate them later. I have printed the graph to make sure everything
> is as I would expect. Primary keys are generated by
> OpenJPA correctly. It even sets foreign key values correctly. SQL logs
> shows its a statement ordering issue.
>
> When I completely dumb down the model and drop foreign key constraints
> then it works - but only sometimes. Generated SQL log shows statement
> order is random at best. Retry works sometimes. I haven't dropped all
> constraints and tried persisting entire model. All my foreign keys are not
> null and I don't want to change my design.
>
> Even when I do manual persist walking down the tree in right order,
> inserts are done in random order (even though I have read OpenJPA does not
> reordering of its own) and fails because of foreign keys.
>
> I am running OpenEJB 3.0 embedded inside Tomcat which no change to
> original configuration. Database is Postgres 8.1.
>
> What am I missing ?
>
> Any help would be appreciated.
>
> thank you,
>
> -Sanjay Kumar
>
>
--
View this message in context: http://www.nabble.com/Foreign-key-violation-while-persisting---statement-order-issue.-tp18062156p18093612.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.