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

后端 未结 25 1211
说谎
说谎 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:38

    I would strongly recommend field access and NOT annotations on the getters (property access) if you want to do anything more in the setters than just setting the value (e.g. Encryption or calculation).

    The problem with the property access is that the setters are also called when the object is loaded. This has worked for me fine for many month until we wanted to introduce encryption. In our use case we wanted to encrypt a field in the setter and decrypt it in the getter. The problem now with property access was that when Hibernate loaded the object it was also calling the setter to populate the field and thus was encrypting the encrypted value again. This post also mentions this: Java Hibernate: Different property set function behavior depending on who is calling it

    This has cause me headaches until I remembered the difference between field access and property access. Now I have moved all my annotations from property access to field access and it works fine now.

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

    Here's a situation where you HAVE to use property accessors. Imagine you have a GENERIC abstract class with lots of implementation goodness to inherit into 8 concrete subclasses:

    public abstract class Foo<T extends Bar> {
    
        T oneThing;
        T anotherThing;
    
        // getters and setters ommited for brevity
    
        // Lots and lots of implementation regarding oneThing and anotherThing here
     }
    

    Now exactly how should you annotate this class? The answer is YOU CAN'T annotate it at all with either field or property access because you can't specify the target entity at this point. You HAVE to annotate the concrete implementations. But since the persisted properties are declared in this superclass, you MUST used property access in the subclasses.

    Field access is not an option in an application with abstract generic super-classes.

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

    I have solved lazy initialisation and field access here Hibernate one-to-one: getId() without fetching entire object

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

    AccessType.PROPERTY: The EJB persistence implementation will load state into your class via JavaBean "setter" methods, and retrieve state from your class using JavaBean "getter" methods. This is the default.

    AccessType.FIELD: State is loaded and retrieved directly from your class' fields. You do not have to write JavaBean "getters" and "setters".

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

    You should choose access via fields over access via properties. With fields you can limit the data sent and received. With via properties you can send more data as a host, and set G denominations (which factory set most of the properties in total).

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

    I prefer field access, because that way I'm not forced to provide getter/setter for each property.

    A quick survey via Google suggests that field access is the majority (e.g., http://java.dzone.com/tips/12-feb-jpa-20-why-accesstype).

    I believe field access is the idiom recommended by Spring, but I can't find a reference to back that up.

    There's a related SO question that tried to measure performance and came to the conclusion that there's "no difference".

    0 讨论(0)
提交回复
热议问题