I'm pretty sure I have found the answer to my own question here
http://www.vogella.de/articles/JavaPersistenceAPI/article.html
"JPA can use either your instance variables (fields) or the
corresponding getters and setters to access the fields. If you want to
use the setter and getter methods the Java class must follow the Java
Bean naming conventions.
If JPA should access directly the instance variables then you annotate
the variable. If you want to use the getter and setting you annotate
them. You are not allowed to mix both methods."
But I am still not sure about the best way to handle maintaining my
bidirectional relations... I could modify all my entities and put
annotations on fields, and do what it suggested in the link I posted
below.. but I am worried about the overhead of loading collections for
no reason, maybe if there was a way to check of a collection was loaded
already?? for example...
if (contact.getTasklistCollection() != null &&
!contact.getTasklistCollection().contains(this))
{
contact.getTasklistCollection().add(this);
}
but I am pretty sure contact.getTasklistCollection() will cause the
collection to be loaded...
any advice on this issue would be greatly appreciated!!!
Derek
On 10-07-16 01:55 PM, Derek Knapp wrote:
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);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
|