Master java skills

First Hibernate Application (using annotations)

Before going through more theory, let’s jump to create our first Hibernate application.

  1. Create a maven project
  2. Add following dependencies to your pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sks</groupId>
	<artifactId>HibernateExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.0.0.Alpha5</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>19.3</version>
    </dependency>
</dependencies>

</project>

3. Create a persistent class called Doctor. Persistent class means the class which will be persisted into a corresponding database table.

package com.sks;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Doctor")
public class Doctor {
	@Id
	private int id;
	private String name;
	private String department;
//getters and setters
//constructor using fields
//constructor from super class
}

5. Create hibernate config file which contains data required to connect to a database and mapping resource location

hibernate.cfg.xml file for Oracle

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC  
        "-//Hibernate/Hibernate Configuration DTD 6.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-configuration-6.0.dtd">
        
<hibernate-configuration>

<session-factory>
	<property name="hbm2ddl.auto">update</property>
	<property name="dialect">org.hibernate.dialect.OracleDialect</property>
	<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
	<property name="connection.url">jdbc:oracle:thin:@localhost:1521/xepdb1</property>
	<property name="connection.username">your-user-name</property>
	<property name="connection.password">password</property>
	<mapping class="com.sks.Doctor"/>
</session-factory>

</hibernate-configuration>

hibernate.cfg.xml file for mysql

<?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="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/employee_db</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">password</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="show_sql">true</property>
        <mapping class="com.sks.Doctor"/>   
   	</session-factory>  
  
</hibernate-configuration>

6. Create the main method class

package com.sks;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class DoctorMain {
	
	public static void main(String[] args) {
		
		StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
        Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
     	
		SessionFactory factory = meta.buildSessionFactory();
		Session session = factory.openSession();
		Transaction t1 = session.beginTransaction();
		
		Doctor doc1 = new Doctor(101, "Akash", "Dermatology");
		session.save(doc1);
		t1.commit();
		
		System.out.println("Doctor object saved successfully.");
		session.close();
		factory.close();
	}
}

7. Before running the application create a table in the database with name ‘Doctor’ with three properties id, name, and department.

8. Run the application as a Java Application and check the record in the database.