Master java skills

First Hibernate Application (using xml configuration)

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>
    
    <!--<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>8.0.30</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;

public class Doctor {
	
	private int id;
	private String name;
	private String department;
//getters and setters
//constructor using fields
//constructor from super class
}

4. Create mapping file for Doctor class

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 6.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.sks.Doctor" table="Doctor">

		<id name="id">
			<generator class="assigned"></generator>
		</id>
		<property name="name"></property>
		<property name="department"></property>
	</class>

</hibernate-mapping>

5. Create hibernate config file which contains data required to connect to a database (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-3.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 resource="doctor.hbm.xml"/>
</session-factory>

</hibernate-configuration>

If you are using MySQL database, use the below file

<?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-3.0.dtd">

<hibernate-configuration>

	<session-factory>
		<property name="hbm2ddl.auto">update</property>
		<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
		<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/saras_db</property>
		<property name="connection.username">root</property>
		<property name="connection.password">abcd</property>
		<mapping resource="doctor.hbm.xml" />
	</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. Run the application as a Java Application and check the record in the database.