Hibernate Annotations - Which is better, field or property access?

后端 未结 25 1209
说谎
说谎 2020-11-22 15:02

This question is somewhat related to Hibernate Annotation Placement Question.

But I want to know which is better? Access via properties or access vi

相关标签:
25条回答
  • 2020-11-22 15:28

    I prefer accessors, since I can add some business logic to my accessors whenever I need. Here's an example:

    @Entity
    public class Person {
    
      @Column("nickName")
      public String getNickName(){
         if(this.name != null) return generateFunnyNick(this.name);
         else return "John Doe";
      }
    }
    

    Besides, if you throw another libs into the mix (like some JSON-converting lib or BeanMapper or Dozer or other bean mapping/cloning lib based on getter/setter properties) you'll have the guarantee that the lib is in sync with the persistence manager (both use the getter/setter).

    0 讨论(0)
  • 2020-11-22 15:28

    Another point in favor of field access is that otherwise you are forced to expose setters for collections as well what, for me, is a bad idea as changing the persistent collection instance to an object not managed by Hibernate will definitely break your data consistency.

    So I prefer having collections as protected fields initialized to empty implementations in the default constructor and expose only their getters. Then, only managed operations like clear(), remove(), removeAll() etc are possible that will never make Hibernate unaware of changes.

    0 讨论(0)
  • 2020-11-22 15:29

    I prefer fields, but I've run into one situation that seems to force me to place the annotations on getters.

    With the Hibernate JPA implementation, @Embedded doesn't seem to work on fields. So that has to go on the getter. And once you put that on the getter, then the various @Column annotations have to go on the getters too. (I think Hibernate doesn't want mixing fields and getters here.) And once you're putting @Column on getters in one class, it probably makes sense to do that throughout.

    0 讨论(0)
  • 2020-11-22 15:29

    i thinking about this and i choose method accesor

    why?

    because field and methos accesor is the same but if later i need some logic in load field, i save move all annotation placed in fields

    regards

    Grubhart

    0 讨论(0)
  • 2020-11-22 15:31

    To make your classes cleaner, put the annotation in the field then use @Access(AccessType.PROPERTY)

    0 讨论(0)
  • 2020-11-22 15:32

    That really depends on a specific case -- both options are available for a reason. IMO it boils down to three cases:

    1. setter has some logic that should not be executed at the time of loading an instance from a database; for example, some value validation happens in the setter, however the data coming from db should be valid (otherwise it would not get there (: ); in this case field access is most appropriate;
    2. setter has some logic that should always be invoked, even during loading of an instance from db; for example, the property being initialised is used in computation of some calculated field (e.g. property -- a monetary amount, calculated property -- a total of several monetary properties of the same instance); in this case property access is required.
    3. None of the above cases -- then both options are applicable, just stay consistent (e.i. if field access is the choice in this situation then use it all the time in similar situation).
    0 讨论(0)
提交回复
热议问题