persistence@glassfish.java.net

Re: delay in ManyToOne relationship updates

From: christopher delahunt <christopher.delahunt_at_oracle.com>
Date: Thu, 24 Sep 2009 09:21:29 -0400

Hello,

JPA does not maintain relationships for you, so the best way to ensure
that the child is in the parent's list is to add it. When you set the
child's parent, also add the child to the parent's list of children.

Otherwise, you must flush the database and then refresh the parent for
the database, causing unnecessary database hits. This is why you also
see the child in the parent's children on restart, since it causes
the parent to be brought in from the database - essentially the same as
a refresh.

Best Regards,
Chris

josu wrote:
> Hello.
>
> I am developping an application using Toplink 2.0.1 persistence and
> Glassfish 2.1.
>
> I have an entity with a OneToMany/ManyToOne relationship to itself (an
> article can have a single father-article, and a father can have several
> item-articles), defined like this:
>
> @Entity
> public class Article implements java.io.Serializable, java.lang.Comparable{
>
> ...
>
> private Article father;
> private List<Article> items;
>
> ...
>
> @ManyToOne
> public Article getFather(){
> return father;
> }
>
> public void setFather(Article father){
> this.father=father;
> }
>
> @OneToMany(mappedBy="father")
> public List<Article> getItems(){
> return items;
> }
>
> public void setItems(List<Article> items){
> this.items=items;
> }
> }
>
> There are more properties in the entity but i don't mention them as they
> aren't relevant.
>
> The issue is that when I set an article's father, father's item list is not
> updated inmediatly. In fact, I don't get it updated until I restart
> Glassfish.
>
> How could I force that update be made inmediatly?
>
> Thanks.
>