Hibernate environment setup in Eclipse

In this post we are going to perform  Hibernate environment setup  in Eclipse IDE.

Hibernate environment setup in Eclipse

So first of all. Download latest version of Hibernate 

Click Here to Download

1. Create Java Project.

2. Right click on java Project and select Build path and Select Add external jar

3. Add All Hibernate  Required jar file.

Hibernate environment setup

Writing Hibernate program

  • Create configuration file Hibernate.cfg.xml (Make sure You created this file under src folder
  • Create Mapping class( POJO class)
  • Create Main class
Let’s Create Simple hibernate program save Object in database.

Hibernate.cfg.xml

[pastacode lang=”markup” manual=”%3C%3Fxml%20version%3D’1.0’%20encoding%3D’utf-8’%3F%3E%0A%0A%3C!DOCTYPE%20hibernate-configuration%20PUBLIC%0A%20%22-%2F%2FHibernate%2FHibernate%20Configuration%20DTD%203.0%2F%2FEN%22%0A%20%22http%3A%2F%2Fwww.hibernate.org%2Fdtd%2Fhibernate-configuration-3.0.dtd%22%3E%0A%0A%3Chibernate-configuration%3E%0A%0A%20%3Csession-factory%3E%0A%0A%20%3C!–%20Database%20connection%20settings%20–%3E%0A%20%3Cproperty%20name%3D%22connection.driver_class%22%3Ecom.mysql.jdbc.Driver%3C%2Fproperty%3E%0A%20%3Cproperty%20name%3D%22connection.url%22%3Ejdbc%3Amysql%3A%2F%2Flocalhost%3A3306%2Ftest%3C%2Fproperty%3E%0A%20%3Cproperty%20name%3D%22connection.username%22%3Eroot%3C%2Fproperty%3E%0A%20%3Cproperty%20name%3D%22connection.password%22%3Eroot%3C%2Fproperty%3E%0A%0A%20%3C!–%20JDBC%20connection%20pool%20(use%20the%20built-in)%20–%3E%0A%20%3Cproperty%20name%3D%22connection.pool_size%22%3E10%3C%2Fproperty%3E%0A%0A%20%3C!–%20Drop%20and%20re-create%20the%20database%20schema%20on%20startup%20–%3E%0A%20%3Cproperty%20name%3D%22hbm2ddl.auto%22%3Eupdate%3C%2Fproperty%3E%0A%0A%0A%20%3C!–%20SQL%20dialect%20–%3E%0A%20%3Cproperty%20name%3D%22dialect%22%3Eorg.hibernate.dialect.MySQLDialect%3C%2Fproperty%3E%0A%0A%20%3C!–%20Disable%20the%20second-level%20cache%20–%3E%0A%20%3Cproperty%20name%3D%22cache.provider_class%22%3Eorg.hibernate.cache.NoCacheProvider%3C%2Fproperty%3E%0A%0A%20%3C!–%20Echo%20all%20executed%20SQL%20to%20stdout%20–%3E%0A%20%3Cproperty%20name%3D%22show_sql%22%3Etrue%3C%2Fproperty%3E%0A%0A%20%0A%20%3Cmapping%20class%3D%22com.spy.hibernate.First.Myphonebook%22%2F%3E%0A%20%0A%20%3C%2Fsession-factory%3E%0A%0A%20%3C%2Fhibernate-configuration%3E” message=”” highlight=”” provider=”manual”/]

Java Bean class( POJO class)

[pastacode lang=”java” manual=”package%20com.spy.hibernate.First%3B%0A%0Aimport%20javax.persistence.Entity%3B%0Aimport%20javax.persistence.GeneratedValue%3B%0Aimport%20javax.persistence.GenerationType%3B%0Aimport%20javax.persistence.Id%3B%0Aimport%20javax.persistence.Table%3B%0A%0A%40Entity%0A%40Table(name%3D%22Contactbook%22)%0Apublic%20class%20Myphonebook%20%0A%7B%0A%09%09%40Id%20%40GeneratedValue(strategy%3DGenerationType.AUTO)%0A%09%0A%20%20%20%20int%20id%3B%0A%09%0A%09String%20name%3B%0A%09String%20mobilenumber%3B%0A%09%09%0A%09public%20int%20getId()%20%0A%09%7B%0A%09%09return%20id%3B%0A%09%7D%0A%0A%09public%20void%20setId(int%20id)%20%0A%09%7B%0A%09%09this.id%20%3D%20id%3B%0A%09%7D%0A%0A%09public%20String%20getName()%20%0A%09%7B%0A%09%09return%20name%3B%0A%09%7D%0A%09public%20void%20setName(String%20name)%20%0A%09%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%0A%09public%20String%20getMobilenumber()%20%0A%09%7B%0A%09%09return%20mobilenumber%3B%0A%09%7D%0A%0A%09public%20void%20setMobilenumber(String%20mobilenumber)%0A%09%7B%0A%09%09this.mobilenumber%20%3D%20mobilenumber%3B%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Main class to Save object into database.

[pastacode lang=”java” manual=”package%20com.spy.hibernate.First%3B%0A%0Aimport%20org.hibernate.Session%3B%0Aimport%20org.hibernate.SessionFactory%3B%0Aimport%20org.hibernate.cfg.Configuration%3B%0A%0Apublic%20class%20CreatePhonebook%20%0A%7B%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%09%7B%0A%09%0A%09%20%20%20Myphonebook%20contact%3Dnew%20Myphonebook()%3B%0A%09%09%0A%09%20%20%20contact.setMobilenumber(%228866766995%22)%3B%0A%09%20%20%20contact.setName(%22Bhavesh%22)%3B%0A%09%09%0A%09%20%20%20%20SessionFactory%20sf%3Dnew%20Configuration().configure().buildSessionFactory()%3B%0A%09%09%0A%09%20%20%20%20Session%20session%3Dsf.openSession()%3B%0A%09%09%0A%09%20%20%20%20session.beginTransaction()%3B%0A%09%20%20%20%20%0A%09%20%20%20%20session.save(contact)%3B%0A%09%09%0A%09%20%20%20%20session.getTransaction().commit()%3B%0A%09%09%0A%20%20%20%20%20%20%20%09%20%20%20%20session.close()%3B%09%09%09%09%09%0A%09%7D%09%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
Output: Hibernate: insert into Myphonebook (mobilenumber, name) values (?, ?)

See in your mysql Database using query: Select *from Myphonebook;

createphonebook

Let’s understand Some of Hibernate annotation

@Entity
This annotation used to mark class as Entity.when You mark class as an Entity it Creates database table and Gives table name same as a class name.


@Entity(name=”Contactbook”)
It has only one Attribute called name.


@Table(name=”tablename”)
This annotation used to give a table name.


@Id
This annotation used to mark field as a primary key.


@Id @GeneratedValue(strategy=GenerationType.AUTO)
This annotation used to Generate Auto increment primary key.


@Transient
Hibernate Automatically create table by Seeing your member Variable of Class But if you want that
Some Variable not Save in Database Then Mark those field as a Transient.


@Temporal(TemporalType.Date)
This annotation used change Date format
Default it display time, date
But if you want only date will display then you have to use above Annotation.


@Lob
It means large object. When you save object in database there are some Default size. Suppose for varchar have 255 char. But some field like Address, description require More space so we mark those a large object using above annotation.

 

Click Here to learn JDBC

One Reply to “Hibernate environment setup in Eclipse”

Leave a Reply