Hibernate - SPLessons

Hibernate Annotation

Home > Lesson > Chapter 9
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate Annotation

Hibernate Annotation

shape Description

Hibernate Annotations are used to reduce the code of an application and the main advantage of an annotation is that no need to write mapping file, Annotation tags will be represented with "@", The following are the tags which will be used in the code. Hibernate Annotation will carry meta data that will also be used in the code.

@Entity

It means the Persistence class is an entity. This Hibernate Annotation is represented in class-level.

@Table

@Table it is also class level hibernate annotation, Here if the developer did not mention any table name, then by default it will be taken to the table name.

@Id

@Id is represented on Property level or getter method level annotation. It is used for mapping a POJO class variable and Primary Key Column of the DataBase table.

@Column

@Column is to map with a property column of DataBase table. If property name and column name same then @Column is optional. If the developer did not mention any column, then by default it will be taken.

shape Example

Following are the required files to execute the hibernate application, Here hibernate mapping file is not needed. Employeeannotation.java [java]package com.abc; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name= "emp500") public class Employeeannotation { @Id private int id; private String firstName,lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } [/java] The @Table annotation permits the programmer to determine the points of interest of the table that will be utilized to continue the entity in the database.The @Table comment gives four characteristics, permitting the programmer to supersede the name of the table, its index, and its mapping, and uphold one of a kind limitations on segments in the table. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, one can have control over the public interface. hibernate.cfg.xml [java]<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <mapping class="com.abc.Employeeannotation"/> </session-factory> </hibernate-configuration>[/java]
Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.
Here Write <mapping class="com.abc.Employeeannotation"/> to identify the file. persistencestored.java [java]package com.abc; import org.hibernate.*; import org.hibernate.cfg.*; public class persistencestored { public static void main(String[] args) { Session session=new AnnotationConfiguration() .configure().buildSessionFactory().openSession(); Transaction t=session.beginTransaction(); Employeeannotation e1=new Employeeannotation(); e1.setId(1001); e1.setFirstName("sonoo"); e1.setLastName("jaiswal"); Employeeannotation e2=new Employeeannotation(); e2.setId(1002); e2.setFirstName("vimal"); e2.setLastName("jaiswal"); session.persist(e1); session.persist(e2); t.commit(); session.close(); System.out.println("successfully saved"); } } [/java] The commit() treats a solitary SQL statement or a gathering of SQL explanations as one logical unit, and if the statement fails, the entire transaction comes up short. To empower manual-transaction support rather than the auto-commit mode that the JDBC driver utilizes uses by default. Output: Output will be as follows, The following message will be displayed and table will be created automatically in MySQL. As inserted records in the coding output will be as follows.

Summary

shape Key Points

  • If the developer use hibernate annotation, Then no need to use mapping file.
  • Class name should represent in configuration file, Then it can identify easily.
  • If not use @table tag, Table name will be created by default.