0% found this document useful (0 votes)
16 views

Hibernate - Practical - 1 (XML Based)

Uploaded by

Manoj Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Hibernate - Practical - 1 (XML Based)

Uploaded by

Manoj Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Create a simple table creation application using Hibernate.

( XML based
configuration )

Create a Maven ( Quick start project ) and mentioned the group id ( com.hibernate
) , and artifactid xmlexample1

Create a model name HP_Student in com.hibernate package under src/main/java

package < Package name >;

public class HP_Student {


public HP_Student() {
super();
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
private int studentId;
private String firstName;
private String lastName;
}
Then, create a package name resources under src/main/java folder and add file with
name as a 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">
<!-- Main configuration file -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.cj.jdbc.Driver
</property>

<property name="hibernate.connection.url">
jdbc:mysql://localhost:3307/classicmodels
Mentioned connection URL is according to your machine
</property>

<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
password
</property>

<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update </property>

<mapping resource="resources/HP_Student.hbm.xml" />


</session-factory>
</hibernate-configuration>
Then add the below dependency in pom.xml file between

<dependencies>
---------------------
</dependencies>

<!-- https://mvnrepository.com/artifact/mysql/mysql-
connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.hibernate/hibernate-
core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
Then, add file with the name as a HP_Student.hbm.xml in resources package.

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-
3.0.dtd">
<hibernate-mapping>
<class
name="com.hibernate.practices.XMLExample1.HP_Student"
table="HP_Student_5a10">
<id name="studentId" type="int" column="student_Id">
<generator class="assigned"/>
</id>
<property name="firstName" type="string">
<column name="first_Name"/>
</property>
<property name="lastName" type="string">
<column name="last_Name"/>
</property>
</class>
</hibernate-mapping>

Now create a class name with HibernateUtil_Ex1 under above created package (
com.hibernate.<Project name> ).
package < Package Name >

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil_Ex1 {

private static final SessionFactory sessionFactory =


buildSessionFactory();

private static SessionFactory buildSessionFactory() {


SessionFactory sessionFactory = null;
try {
Configuration configuration = new
Configuration();

configuration.configure("resources/hibernate.cfg.xml");
sessionFactory =
configuration.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}

public static SessionFactory getSessionFactory()


{
return sessionFactory;
}
}

Then add the below code in App.Java file…..


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class App


{
@SuppressWarnings("deprecation")
public static void main( String[] args )
{
HP_Student student = new HP_Student();
student.setStudentId(2);
student.setFirstName("Vedant");
student.setLastName("Thakar");

Session session =

HibernateUtil_Ex1.getSessionFactory().openSession();

Transaction tran = null;

try {
tran = session.beginTransaction();
session.save(student);
tran.commit();
}
catch (HibernateException e) {
if(tran==null) {
tran.rollback();
}
e.printStackTrace();
}
finally {
session.close();
}
}
}

You might also like