You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by JayaPrakash <jp...@sify.com> on 2010/05/01 20:59:34 UTC

Forcing an Object to be always inserted/updated first during persist/merge

Hi All,

        In an Object tree, is there a way I can force a particular object to
be always inserted/updated first during persist/merge operation?

For example in an object tree like A -> B -> C -> D, and B, C and D have
their corresponding children. In this scenario I want always the A objects
Inserts/Updates to happen first. I dont really care how what order is
followed after that.

Thanks,
JP
                                                    
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Forcing-an-Object-to-be-always-inserted-updated-first-during-persist-merge-tp4990960p4990960.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Forcing an Object to be always inserted/updated first during persist/merge

Posted by Ян Программист <we...@gmail.com>.
Interesting idea is to use a message broker to create complex
commit/rollback rules between UpdateManager instances on separated machines
(runtimes) for dealing with managed Java applications. John

2010/5/3 JayaPrakash <jp...@sify.com>

>
> Hi Michael,
>
>          Thanks for your reply. I already have that configuration in place,
> but the problem is that, since I am using the @ElementDependent for orphan
> deletion, to honor the SQL order while delete, the OpenJPA is updating the
> child table foreignkey values to null, since my DB does not allow nulls in
> to foreignkey columns, i get an error.
>
> So, I was wondering if there is a way that I can force a particular object
> to be inserted/updated first before all the other objects in the Tree.
> Since, the object that is at the top most level needs to be inserted first
> because of some requirement that we have.
>
> Thanks,
> JP
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Forcing-an-Object-to-be-always-inserted-updated-first-during-persist-merge-tp4990960p4999545.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Forcing an Object to be always inserted/updated first during persist/merge

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  Did you try orphan deleting from the leaf?

  Say A has many B and B has many C and C has many D

  Then some thing like this in A.java

   @OneToMany(...., orphanDelete=true)
   List bs;
   void deleteChildren() {
      for (B b : bs) b.deleteChildren();
      bs = null;
   }

   And in B.java

   @OneToMany(...., orphanDelete=true)
   List<C> cs;
   void deleteChildren() {
      for (C c : cs) c.deleteChildren();
      cs = null;
   }


And then at the application level,

   A a = ...;
  a.deleteChildren();

   

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Forcing-an-Object-to-be-always-inserted-updated-first-during-persist-merge-tp4990960p5006108.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Forcing an Object to be always inserted/updated first during persist/merge

Posted by JayaPrakash <jp...@sify.com>.
Hi Michael,

          Thanks for your reply. I already have that configuration in place,
but the problem is that, since I am using the @ElementDependent for orphan
deletion, to honor the SQL order while delete, the OpenJPA is updating the
child table foreignkey values to null, since my DB does not allow nulls in
to foreignkey columns, i get an error. 

So, I was wondering if there is a way that I can force a particular object
to be inserted/updated first before all the other objects in the Tree.
Since, the object that is at the top most level needs to be inserted first
because of some requirement that we have.

Thanks,
JP
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Forcing-an-Object-to-be-always-inserted-updated-first-during-persist-merge-tp4990960p4999545.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Forcing an Object to be always inserted/updated first during persist/merge

Posted by Michael Dick <mi...@gmail.com>.
Hi,

The openjpa.UpdateManager configuration property  can be used to control the
order that SQL statements are executed. There are really two options for
this property, OperationOrder and Constraint. These options are further
separated by whether they support SQL batching - so there's also a
BatchingOperationOrder and BatchingConstraint. The batching variations do
not change the order of SQL though - they just submit batch operations when
possible.

The OperationOrder update manager (and BatchingOperationOrder) execute SQL
statements in the same order that the application calls em.persist,
em.merge, em.remove, and so on. So if you use the OperationOrder update
manager you can ensure A gets inserted first by calling em.persist(A), and
then do whatever other work you want to do.

The Constraint update manager (and BatchingConstraint) may reorder the SQL
operations (at commit / flush time) to ensure any FK constraints are
honored. The catch here is that OpenJPA can only reorder for the constraints
it knows about. By default OpenJPA does not assume that there is a
ForiegnKey constraint but you can use the @ForeignKey to tell OpenJPA that
there is a constraint.

So, the short answer is that you probably want to use the OperationOrder or
BatchingOperationOrder update manager. Just add this to persistence.xml :

<property name="openjpa.UpdateManager" value="batching-operation-order"/>

Alternatively if you have ForeignKeys in your database then you could use
@ForeignKey and OpenJPA will reorder the SQL 'automatically'.

HTH,
-mike

On Sat, May 1, 2010 at 1:59 PM, JayaPrakash <jp...@sify.com> wrote:

>
> Hi All,
>
>        In an Object tree, is there a way I can force a particular object to
> be always inserted/updated first during persist/merge operation?
>
> For example in an object tree like A -> B -> C -> D, and B, C and D have
> their corresponding children. In this scenario I want always the A objects
> Inserts/Updates to happen first. I dont really care how what order is
> followed after that.
>
> Thanks,
> JP
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Forcing-an-Object-to-be-always-inserted-updated-first-during-persist-merge-tp4990960p4990960.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>