Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You place them either on the <em>field</em> or on the <em>getter</em>. From the Hibernate Annotations Reference Guide:</p> <blockquote> <h2><a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-entity" rel="nofollow noreferrer">2.2.1. Marking a POJO as persistent entity</a></h2> <p>(...)</p> <p>Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.</p> </blockquote> <p>You might also want to read about the <code>@Access</code> annotation that allows to force/override the access type (prior to Hibernate Annotations 3.5 and JPA 2.0, it was part of Hibernate Annotation Extensions):</p> <blockquote> <h3><a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e535" rel="nofollow noreferrer">2.2.2.2. Access type</a></h3> <p>By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter. That works well in practice and is the recommended approach.</p> <blockquote> <p><strong>Note</strong></p> <p>The placement of annotations within a class hierarchy has to be consistent (either field or on property) to be able to determine the default access type. It is recommended to stick to one single annotation placement strategy throughout your whole application.</p> </blockquote> <p>However in some situations, you need to:</p> <ul> <li>force the access type of the entity hierarchy</li> <li>override the access type of a specific entity in the class hierarchy</li> <li>override the access type of an embeddable type</li> </ul> <p>The best use case is an embeddable class used by several entities that might not use the same access type. In this case it is better to force the access type at the embeddable class level.</p> <p>(...)</p> </blockquote> <p>Regarding the pros and cons of both styles, I suggest to read the following questions:</p> <ul> <li><a href="https://stackoverflow.com/questions/942035/hibernate-jpa-annotating-bean-methods-vs-fields">Hibernate/JPA - annotating bean methods vs fields</a></li> <li><a href="https://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access">Hibernate Annotations - Which is better, field or property access?</a></li> <li><a href="https://stackoverflow.com/questions/332591/performance-difference-between-annotating-fields-or-getter-methods-in-hibernate">Performance difference between annotating fields or getter methods in Hibernate / JPA</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload