Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annotations on instance declaration or get method using JPA in entities?

 
Kenneth Gustafsson
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I first thought it was neat to have the JPA annotations on the instance variable declarations in my entities. That way they're all neatly together and are easy to get an overview of at the beginning of my class definitions.

Then I heard that the recommended way is to have the JPA annotations on the get-methods related to the instance variables. Supposedly they will make JPA use my getters and setters to change my instance variables instead of going "directly" to them (which allows for no logic and has less encapsulation).

Is this true? How do you do it and why?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you do it? Well, an example speaks a thousand words:


@Entity
@Table(name = "BlobClobBoBob", schema = "examscam")

public class BlobClobber {
Long id;
String beans;
char[] ming;
Character[] witness;
java.sql.Clob sqlClob;
java.sql.Blob sqlBlob;
byte[] me;

@Id
@GeneratedValue
@Column(name = "id")

public Long getId() {return id;}
@Lob
public String getBeans() {return beans;}
@Lob
public byte[] getMe() {return me;}
@Lob
public char[] getMing() {return ming;}
@Lob
public java.sql.Blob getSqlBlob() {return sqlBlob;}
@Lob
public java.sql.Clob getSqlClob() {return sqlClob;}
@Lob
public Character[] getWitness() {return witness;}


}


But this is definitely the trend in the industry. Pretty much any of the reference material you have picked up in the last little while will decorate the accessors and mutators, not the variables themselves.

-Cameron McKenzie
[ May 08, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Kenneth Gustafsson
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input. I would guess you only add annotations to getters and not mutators?

Also, is there any specific reason for this trend? Is there a difference in what actually happens between writing the annotation at the accessor and writing it at the variable?
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cameron Wallace McKenzie:

But this is definitely the trend in the industry. Pretty much any of the reference material you have picked up in the last little while will decorate the accessors and mutators, not the variables themselves.


Huh??? This is definitely NOT "the trend in the industry". Annotating the fields is pretty much accepted and recognized as the simplest and best way to annotate persistent classes. Field access offers simplicity, locality, error reduction, performance, flexibility, and I could go on.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JPA specification:


When annotations are used, the placement of the mapping annotations on either the persistent fields or persistent properties of the entity class specifies the access type as being either field- or property-based access respectively.
  • If the entity has field-based access, the persistence provider runtime accesses instance variables directly. All non-transient instance variables that are not annotated with the Transient annotation are persistent. When field-based access is used, the object/relational mapping annotations for the entity class annotate the instance variables.
  • If the entity has property-based access, the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent. The property accessor methods must be public or protected. When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[2].

  •  
    Mike Keith
    author
    Ranch Hand
    Posts: 304
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rodrigo,

    I'm not sure what your point was. Of course we provided both methods of access in the spec because we wanted to ensure that people had choice, but the question asked was which one to use. Different people have different needs, of course, but I had to correct the false statement that property access was the generally accepted trend.
     
    Kenneth Gustafsson
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your input Mike and Rodrigo!

    I think Rodrigo's point was to help me out with one of my questions. I wondered if there were a difference in how persistence is done when annotating on the fields respectively the getters. And he pointed me to the specification.

    Since then I've read the relevant part of the specification myself too, and I really recommend it.

    Field access offers simplicity, locality, error reduction, performance, flexibility

    With all those advantages of using field based access. When is it the right choice to use annotations on the getters? I also wonder which flexibility advantage you get as I have a hard time figuring it out by myself.
     
    Mike Keith
    author
    Ranch Hand
    Posts: 304
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is handy to be able to use method access when you have things like virtual properties (the property represents something in the database, even though the state in the instance may be stored differently). Other cases are when you need validation to occur on database values at instance creation time, etc.

    The flexibility offered by field access means that you don't actually even need to have get/set methods (for example, you often don't want a set method for the identifier field, especially when id generation is used), or if you do want them you can name them whatever you want (e.g. common case is adding an item to a collection and you want an "add" method instead of a "set" method).
     
    Rodrigo Lopes
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's right, I just wanted to help.
    Showed the difference as stated in the specification.
    I didn't say there's a trend.
    Actually I agree that there isn't. It's kind of a personal choice as some say.
    [ June 04, 2008: Message edited by: Rodrigo Lopes ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic