Most Common Annotations Used in Hibernate 5

Here we will discuss most commonly used basic annotations in hibernate - @Entity, @Table, @Id, @GeneratedValue, @Column, @Basic, @Transient, @Temporal and @Lob. Even though many annotations has both JPA (java.persistance) and hibernate (org.hibernate) versions, it is preferred to use JPA annotations whenever possible. As you can see below, some of the hibernate specific annotations are already deprecated.

 

@Entity

  • @Entity annotation (javax.persistence.Entity) over the class tell hibernate to treat this class as our entity class that needs to be saved.

  • The hibernate specific Entity annotation org.hibernate.annotations.Entity has been deprecated in hibernate 4.

    • We have to use annotations corresponding to its attributes/values to get the same result.

      • For instance, @org.hibernate.annotations.Entity(selectBeforeUpdate=true) is replaced by @SelectBeforeUpdate(true).

 

@Entity (name=”XYZ”)

  • This will change the identity name to XYZ.

    • By default, hibernate generate the table name same as classname with only an @Enity annotation.

    • With @Entity(name=”XYZ”) and in the absence of an @Table annotation to change the table name, the entity name will be used to create table instead of class name.

 

@Table(name=”XYZ”))

  • @Table(name=”XYZ”)) annotation (javax.persistence.Table /org.hibernate.annotations.Table) tell hibernate to create table with name XYZ instead of class name.

  • @Table(name=”XYZ”)) is different from @Entity (name=”XYZ”) in that @Table changes only table name and entity name still remains the default, which is class name.

  • One of the applications of this difference between entity names and table names is the use of them within HQL(Hibernate Query Language).

 

@Id

  • The @Id (javax.persistence.Id) over the id field tell hibernate to make this field as the primary key of the table.

 

@GeneratedValue

  • If you use @GeneratedValue, then hibernate will automatically generate values for that using an internal sequence.

    • Therefore you don’t have to set it manually.

  • You can use it along with @Id and make your primary key automatically generated rather than setting each time.

  • You can also specify a strategy on how the values will be generated like

    • @GeneratedValue(strategy=GenerationType.AUTO).

      • AUTO is the default if you don’t give a strategy.

      • AUTO is the preferred option as hibernate will chose the best strategy for us automatically.

    • Other values are IDENTITY, SEQUENCE and TABLE, which are actually dependent on the database that you use.

 

@Column

Can be used to specify column mappings. For example, to change the column name in the associated table in database.

 

@Basic

  • @Basic without any parameters is same as without having it.

  • It just tells hibernate to treat it is a field that needs to be saved and is the same behavior without it.

  • However the use comes when it is used along with its parameters.

  • By default it is same as @Basic (optional=true) 

    • the field may not be supplied any value (not calling getter) or supplied with a null value.

  • @Basic (optional=false) makes it a non-null field.  

    • If not supplied any value (not calling getter) or supplied with a null value with @Basic (optional=false), you will get an exception: org.hibernate.PropertyValueException: not-null property references a null or transient value.

 

@Transient

  • This tells hibernate not to save this field.

    • Even without this annotation, a static variable or a transient variable is not saved.

    • So the behavior is same for a static variable, transient variable or any other variable with @Transient annotation. 

  • If annotations are placed over getters, those placed over fields are ignored and only getters are considered. 

    • Even other fields are ignored and all other getters are considered. 

    • Hence if you have annotations over any of your getter, then a static variable, transient variable or any other variable will be saved as usual if it has a getter.

 

@Temporal

  • @Temporal over a date field tells hibernate the format in which the date needs to be saved. For instance, by default the date will be saved as a timestamp, but to save date alone we can use @Temporal(TemporalType.DATA).

 

@Lob

  • @Lob tells hibernate that this is a large object, not a simple object.

    • So hibernate creates a CLOB or BLOB based on the type of the object.

      • For instance, if @Lob comes over a string, then hibernate assumes to use a character large object (CLOB).

 

Note:

  1. If a class is present in javax.persistence and org.hibernate, it is preferred to import from javax.persistence package.

    • This will make the code more generic and it will be easy to replace hibernate with another framework that implements Java Persistence API.

  2. Annotations on fields usually can be used over the getters too.

    • This is preferred in most cases as it gives more flexibility to change the actual field name or add some extra logic while retrieving the data. 

    • If annotations are placed over getters, those placed over fields are ignored and only getters are considered. 

      • Even other fields are ignored and all other getters are considered. 

        • For instance, put @id over the setter and field, and see for yourself that the one over field is ignored and even other annotations over other fields are ignored.

Tags: 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)