Skip to main content

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

I would recommend always using field access (annotating the fields, not the
methods), for this reason, as well as for performance reasons.  If you
annotate the methods, then the spec requires that we call them.

If you must annotate the methods, then you could have a private method with
no side-effects for JPA, and another marked as @Transient with side-effects
for the application.



Derek Knapp-3 wrote:
> 
> 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);
>>     }
>> }
>> 
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://old.nabble.com/annotate-field-or-method--tp29186338p29336120.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top