delay in ManyToOne relationship updates

View: New views
4 Messages — Rating Filter:   Alert me  

delay in ManyToOne relationship updates

by josu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Re: delay in ManyToOne relationship updates

by christopher delahunt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.
>  

Re: delay in ManyToOne relationship updates

by Wouter van Reeven :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,


Make sure to add the item to the Father's list before you persist the
item by calling

father.getItems().add(item);


HTH, Wouter van Reeven

On Thu, Sep 24, 2009 at 06:18:03AM -0700, 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.
> --
> View this message in context: http://www.nabble.com/delay-in-ManyToOne-relationship-updates-tp25567844p25567844.html
> Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.

--
Lime and limpid green a second scene,
A fight between the blue you once knew.
Floating down the sound resounds
Around the icy waters underground
[Pink Floyd - Astronomy Domine]

Skype: wvreeven
Facebook: wvreeven
Twitter: wvreeven

Re: delay in ManyToOne relationship updates

by josu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I really thought JPA was responsible for maintaining the relationship, so making both asignments would be problematic. But the solution you both gave me is working ok. Thanks for making things clear.


christopher delahunt wrote:
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.
>