Hibernate Hello World Annotations

In this example, we will use Hibernate annotation-based mapping to insert a new record in a database table.

Hibernate annotations is based on JPA specification and supports all its features.

 

1. Technologies used

  • Hibernate 4.3.6.Final
  • MySQL 5.1
  • Maven 3.0.5
  • Eclipse 4.2
  • JDK 1.7

 

2. Create table :

We use the following script to create a table named : CUSTOMER

 

3. Project structure

Hibernate-Hello-World-Annotation

 

4. Project dependencies

 

5. Model class

We create our persistent entity by defining the mapping thought annotations instead of hbm.xml file.

File : src/main/java/com/keylesson/persistence/Customer.java

 

6. Hibernate configuration file

We declare our annotated class in the XML configuration file.

File : src/main/resources/hibernate.cfg.xml

 

7. HibernateUtil

 

8. Test It

We will insert a new record in the CUSTOMER table.

 

Output :

Hibernate: insert into CUSTOMER (NAME, ADDRESS, CREATION_DATE) values (?, ?, ?)

 

Download source code

 

Related posts