Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put hibernate annotations?

Tags:

Where do i put my hibernate annotations?

Is it the line above my instance variable? Or before the getter? Or before the setter? Or doesn't it really matter?

Thanks a lot

like image 321
Chris Avatar asked Jul 20 '10 02:07

Chris


People also ask

Is @column annotation necessary?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

Is @entity annotation mandatory in Hibernate?

The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity. So, if you use annotations for mappings, @Entity is mandated by the specification and Hibernate has to adhere to it.

What is the use of @entity annotation in Hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default. @Id annotation marks the identifier for this entity.


2 Answers

You place them either on the field or on the getter. From the Hibernate Annotations Reference Guide:

2.2.1. Marking a POJO as persistent entity

(...)

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.

You might also want to read about the @Access 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):

2.2.2.2. Access type

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.

Note

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.

However in some situations, you need to:

  • force the access type of the entity hierarchy
  • override the access type of a specific entity in the class hierarchy
  • override the access type of an embeddable type

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.

(...)

Regarding the pros and cons of both styles, I suggest to read the following questions:

  • Hibernate/JPA - annotating bean methods vs fields
  • Hibernate Annotations - Which is better, field or property access?
  • Performance difference between annotating fields or getter methods in Hibernate / JPA
like image 101
Pascal Thivent Avatar answered Nov 04 '22 09:11

Pascal Thivent


It's up to your style. You may put it before the field or before getter. In strict JPA, the annotations on setters are ignored, but I'm not sure if Hibernate follows that.

You either need to be consistent throughout your Entity, or you need to provide an @Access annotation at the top of the class with a default mode, and another @Access before each field/property you wish to deviate from the current class mode.

like image 20
Brian M. Carr Avatar answered Nov 04 '22 10:11

Brian M. Carr