Hibernate 5 example

Here is the example for using the Hibernate with MariaDb database. 

First, we need to create the configuration file that is hibernate.cfg.xml which contains the various configuration values lie database name, database URL, database username, password etc. This file is the one time activity you have to do and after than, just keep adding the beans or entities classes in mapping tag but other part will remain as it is.

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MariaDB53Dialect</property>
  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <mapping class="example.configuration.demo1.datamodel.Student"/>
 </session-factory>
</hibernate-configuration>

 

Create the very simple example, we are using the following entity bean.

package example.configuration.demo1.datamodel;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {
	@Id
	private int id;
	private String name;
	private String email;

	public Student() {
	
	}
	
	public Student(int id, String name, String email) {
		this.id = id;
		this.name = name;
		this.email = email;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}

After having this done, you could test the program as following.

package example.configuration.demo1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import example.configuration.demo1.datamodel.Student;

public class MainClass {
	public static void main(String[] args){
		
		StandardServiceRegistry registry=new StandardServiceRegistryBuilder().configure().build();
		SessionFactory factory=new MetadataSources(registry).buildMetadata().buildSessionFactory();
		Session session=factory.openSession();
		
		Transaction tx=session.beginTransaction();
		Student student=new Student(1, "Adloki Simoy","adlo@gmail.com");
		session.save(student);
		tx.commit();
		session.close();
	}
}

 

Following are libraries required to run this program.

HibernateDemo_src_example_configuration

 

 

Tags