Hibernate program which prints hello and save the entity in Database

Q. Write a simple program which prints hello and save the entity(java class) in the Database?

 

Steps to run below example –

Step 1 – Create a java project(Since we are not using maven for jar/library, need to download the jar and add manually in the project)

Step 2 – Download hibernate jar and add it to project( right-click on project/properties/java build path/libraries/add external jars/ browse and add).

Step 3 – define the below classes and run the main class(Don’t forget to change the username and password in hibernate.cfg.xml for the database, that you have given during installation of Oracle in your machine ).

Let’s see a simple hibernate program which save the entity in database. We have three files Book.java, HibernateSimpleExample.java and hibernate.cfg.xml.

Book.java – 

package javatute;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity
@Table(name = "book")
public class Book {
	 
	 @Id
	 @GeneratedValue(strategy = GenerationType.AUTO)
	 private int bookId;
	 
	 @Column(name="book_name")
	 private String bookName;

	public String getBookName() {

		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	
	 
	 
}

HibernateSaveEntityExample .java – 

package hibernateannotation;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class HibernateSaveEntityExample {

	public static void main(String[] args) {
	
		Configuration cfg = new Configuration().configure();
		
		SessionFactory factory = cfg.buildSessionFactory();
		
		Session session = factory.openSession();
		
		Book book = new Book();
		
		book.setBookId(109);
		
		book.setBookName("Rich Dad Poor Dad");
		
		Transaction transaction = session.beginTransaction();
		
		session.save(book);
		
		System.out.println("Hello! Book object has been saved");
		
		transaction.commit();
		
		session.close();
		
		factory.close();
	}
}

hibernate.cfg.xml – 

<?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">
 
<hibernate-configuration>

<session-factory>

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

<property name="connection.username">SYSTEM</property>

<property name="connection.password">oracle</property>
 
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">create</property>

 <mapping class="onetomany.Book" />

 <mapping class="onetomany.Story" />
 
</session-factory>

</hibernate-configuration>