-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: annotations
PostPosted: Fri Mar 23, 2007 12:31 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
As everybody knows annotations can be placed on the field or the method. In the former i.e. the field, the hibernate used the reflection to get and set the attribute, if the latter approach is used the hibernate framework will use the accessor and mutator method.

What is the best practise? reflection used to be slow but I believe it is not as slow anymore?

Comments welcomed


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
both are reflection. So it's a matter of taste. I like getters because of the encapsulation

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:27 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Thanks for that, excuse my ignorance but when you say both use reflection, do you mean that both use reflection to access the field directly? i.e. neither never use the accessor or mutator?

According to Pro EJB 3 (Apress) page 73, when annotation is on method "... properties will be made persistent by virtue of the getter and setter methods that exist for them".

Note the reason, I ask this is because I have to put in some extra behaviour in my Entity POJOs. When a number of attributes (mainly primitives and another customer attribute) are at their default value, null has to be inserted into the database. This is for legacy reasons. One way I was going to go about achieving this was to put some extra behavior into the accessors and mutators for the properties - i.e. when the property was null, return the default value to the user. This would be in the hope that hibernate would just never go near the accessor methods and just read the real value (i.e. null) for the property directly and not the the value returned by the accessor method (i.e. the default value).

Currenlty all my annotations are at method level, to change them all so that they are at field level would be big work.

Any help, advice, information greatly greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
both use reflection, field access or getter access. Hibernate comply with the spec, ie use getters when annotations are on getters and fields when annotations are on field

WRT your problem, I see 3 solutions:
- move annotations to field
- use a custom @Type to do your unnatural conversion
- use @AccessType("field") for the specific properties having unnatural conversions

2 and 3 are hibernate specific

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 5:29 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
emmanuel wrote:
both use reflection, field access or getter access. Hibernate comply with the spec, ie use getters when annotations are on getters and fields when annotations are on field

WRT your problem, I see 3 solutions:
- move annotations to field
- use a custom @Type to do your unnatural conversion
- use @AccessType("field") for the specific properties having unnatural conversions

2 and 3 are hibernate specific

Good post, interesting ideas.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.