Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AccessMode in JPA 2

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a expert please verify these two statements:

I use JPA 2.0.

If I annotate the fields, JPA will use reflection to get and set the fields of the entity.

If I annotate the getter methods, then no reflection will be used - JPA uses the getter and setter methods to get and set the property.


Conclusion: Field-based Annotation is less performant than property-based Annotation, because field-based annotation uses reflection and this causes overhead.

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't really about to make decisions about how to write code based on implementations details such as these, are you? Any DB or code tuning will give you much bigger performance improvements than fiddling with stuff like this.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Lester says, it's all implementation details and things like this rarely show noticeable performance impact in a real application.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any DB or code tuning will give you much bigger performance improvements than fiddling with stuff like this.


Indeed, I agree. And cases are that
it rarely show noticeable performance impact



However, is the conclusion correct or not?

Conclusion: Field-based Annotation is less performant than property-based Annotation, because field-based annotation uses reflection and this causes overhead.

I mean, this is the only internal difference of field and property access.

I only search for best practice
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:

However, is the conclusion correct or not?

Conclusion: Field-based Annotation is less performant than property-based Annotation, because field-based annotation uses reflection and this causes overhead.

I mean, this is the only internal difference of field and property access.



No it's not correct. Like I said, it's implementation detail. And infact some of the JPA providers will still use reflection even if your entities have annotated the getters instead of the fields.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thank you!
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this depends on the JPA provider, but most use reflection for both field access and method/property access.

Most JVMs optimize reflection by generating byte code to call the method, so reflection is not always really reflection...

In EclipseLink the opposite is actually true, if weaving is enabled then reflection is not used if field access is used, but is still used for method access.

In general field access gives the JPA provider more control to optimize things.

The main difference in field access vs property access is that using property access you have control of transforming or defaulting the data, so it is more flexible. This is not always a good thing though, a very common problem people have is side-affects in their get/set method cause persistence problems. i.e. setManager adds to the managedEmployees causing the LAZY managedEmployees to instantiate and have duplicate when building an object, or setX assumes Y is not null, but can be when building objects. JPA is not creating new objects, it is restoring an object's state similar to Java serialization (which never uses your get/set methods, and people don't seem to have an issue with that).

In general I always recommend field access. But sometimes property access is required to default or transform data.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello James,

thank you - this helped me to better understand the difference of field and property access.

I am convinced to use field access.

thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic