For Programmers: Free Programming Magazines  


Home > Archive > Java Beans > October 2007 > Toplink EJB and cascade persistence









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Toplink EJB and cascade persistence
tom.simmons@vetco.com

2007-09-26, 8:04 am

I am using EJB3 and Toplink.

I have a parent entity and a child entity that is a one to many, the
child has the cascade=all defined, and the parent has a collection for
all the children that belong to it.

I can happily add children, and call either the create (persist) or
edit (merge) methods in the facades and the parent and children will
correctly be saved to the database, with the correct ID's in the
children pointing to the parent.

What I need to do though is be able to delete a child by saving the
parent if the child has been marked for deletion.

The system doesn't performing the deletes/adds as it goes, but instead
waits until the page is submitted. So what I thought was when I save
the parent, I would be able to add something into create/edit methods
of the child facade class that would check a transient field and if it
said delete would instead call the remove method.

However by adding break points in the the three persistence methods of
the child facade, I can see that when calling the edit of the parent,
none of these methods are used.

Any ideas?

Torsten Jenkner

2007-10-09, 7:07 pm

tom.simmons@vetco.com schrieb:
> I am using EJB3 and Toplink.
>
> I have a parent entity and a child entity that is a one to many, the
> child has the cascade=all defined, and the parent has a collection for
> all the children that belong to it.
>
> I can happily add children, and call either the create (persist) or
> edit (merge) methods in the facades and the parent and children will
> correctly be saved to the database, with the correct ID's in the
> children pointing to the parent.
>
> What I need to do though is be able to delete a child by saving the
> parent if the child has been marked for deletion.
>
> The system doesn't performing the deletes/adds as it goes, but instead
> waits until the page is submitted. So what I thought was when I save
> the parent, I would be able to add something into create/edit methods
> of the child facade class that would check a transient field and if it
> said delete would instead call the remove method.
>
> However by adding break points in the the three persistence methods of
> the child facade, I can see that when calling the edit of the parent,
> none of these methods are used.
>
> Any ideas?
>


I am not quite sure to understand you correctly. But by default the
entity manager does not sync everything immediately but is asynchronous.
i understand that you expect the persistence takes place earlier and not
just when the "page" is submitted. If you wish to have persistence by
your control, you can use the EntityManager's flush() method. But this
should be used very careful, due to the fact that there are reasons
behind the point in time decision of the EntityManager to persist
itself. Doing it yourself can cause malperformance.
Otherwise and the better way is to try to update your "page" with the
updated entity Pojos (in memory) already before they are persisted, as
this is asynchronous this does not mean to be out of sync in any case,
the EntityManager takes care of all necessary persistence.

greetings

Torsten Jenkner
Torsten Jenkner

2007-10-09, 7:07 pm

tom.simmons@vetco.com schrieb:
> I am using EJB3 and Toplink.
>
> I have a parent entity and a child entity that is a one to many, the
> child has the cascade=all defined, and the parent has a collection for
> all the children that belong to it.
>
> I can happily add children, and call either the create (persist) or
> edit (merge) methods in the facades and the parent and children will
> correctly be saved to the database, with the correct ID's in the
> children pointing to the parent.
>
> What I need to do though is be able to delete a child by saving the
> parent if the child has been marked for deletion.
>
> The system doesn't performing the deletes/adds as it goes, but instead
> waits until the page is submitted. So what I thought was when I save
> the parent, I would be able to add something into create/edit methods
> of the child facade class that would check a transient field and if it
> said delete would instead call the remove method.
>
> However by adding break points in the the three persistence methods of
> the child facade, I can see that when calling the edit of the parent,
> none of these methods are used.
>
> Any ideas?
>


I am not quite sure to understand you correctly. But by default the
entity manager does not sync everything immediately but is asynchronous.
i understand that you expect the persistence takes place earlier and not
just when the "page" is submitted. If you wish to have persistence by
your control, you can use the EntityManager's flush() method. But this
should be used very careful, due to the fact that there are reasons
behind the point in time decision of the EntityManager to persist
itself. Doing it yourself can cause malperformance.
Otherwise and the better way is to try to update your "page" with the
updated entity Pojos (in memory) already before they are persisted, as
this is asynchronous this does not mean to be out of sync in any case,
the EntityManager takes care of all necessary persistence.

greetings

Torsten Jenkner
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com