Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] annotate field or method?

I have noticed that eclipse link behaves differently depending on if the field or the getter method is annotated.

In the 1st chunk of code below, setContact task is never called, but in the 2nd one it is called a lot.. is this intended behavior? are their any other differences?

basically I have bi-directional relations that I am trying to keep updated.. so I figured I could add in something like

if (!contact.getTasklistCollection().contains(this))
{
    contact.getTasklistCollection().add(this);
}

in to the setter, but in the 2nd chunk of code, it causes eclipse link to load the TasklistCollection hundreds of times...

I got the idea from
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side


--------------------------------------------------------------------------------------------------------------------------------------------
@Entity
@Table(name = "tasklist")
public class Tasklist implements Serializable
{
    ...
    @JoinColumn(name = "contactid", referencedColumnName = "contactid")
    @ManyToOne
    private Contacts contact;
    ...
    public Contacts getContact()
    {
        return contact;
    }

    public void setContact(Contacts contact)
    {
        this.contact = contact;
        System.out.println("setContact task - " + contact);
    }
}
--------------------------------------------------------------------------------------------------------------------------------------------
@Entity
@Table(name = "tasklist")
public class Tasklist implements Serializable
{
    ...
    private Contacts contact;
    ...
    @JoinColumn(name = "contactid", referencedColumnName = "contactid")
    @ManyToOne
    public Contacts getContact()
    {
        return contact;
    }

    public void setContact(Contacts contact)
    {
        this.contact = contact;
        System.out.println("setContact task - " + contact);
    }
}
--------------------------------------------------------------------------------------------------------------------------------------------


Back to the top