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

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

提交回复
热议问题