openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <ppod...@apache.org>
Subject Re: Foreign key violation while persisting - statement order issue.
Date Tue, 24 Jun 2008 15:32:13 GMT

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.


Mime
View raw message