0% found this document useful (0 votes)
50 views6 pages

Hibernate - Practical - 1 (XML Based)

Uploaded by

Manoj Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views6 pages

Hibernate - Practical - 1 (XML Based)

Uploaded by

Manoj Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Create a simple table creation application using Hibernate.

( XML based
configuration )

Create a Maven ( Quick start project ) and mentioned the group id ( [Link]
) , and artifactid xmlexample1

Create a model name HP_Student in [Link] 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) {
[Link] = studentId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = 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 [Link].

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


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"[Link]
[Link]">
<!-- Main configuration file -->
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">
[Link]
</property>

<property name="[Link]">
jdbc:mysql://localhost:3307/classicmodels
Mentioned connection URL is according to your machine
</property>

<property name="[Link]">
root
</property>
<property name="[Link]">
password
</property>

<property name="[Link]">
[Link]
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="[Link]">update </property>

<mapping resource="resources/HP_Student.[Link]" />


</session-factory>
</hibernate-configuration>
Then add the below dependency in [Link] file between

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

<!-- [Link]
connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!--
[Link]
core -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>
</dependency>
Then, add file with the name as a HP_Student.[Link] in resources package.

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


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"[Link]
[Link]">
<hibernate-mapping>
<class
name="[Link].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 (
[Link].<Project name> ).
package < Package Name >

import [Link];
import [Link];

public class HibernateUtil_Ex1 {

private static final SessionFactory sessionFactory =


buildSessionFactory();

private static SessionFactory buildSessionFactory() {


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

[Link]("resources/[Link]");
sessionFactory =
[Link]();
}
catch (Exception e) {
[Link]();
}
return sessionFactory;
}

public static SessionFactory getSessionFactory()


{
return sessionFactory;
}
}

Then add the below code in [Link] file…..


import [Link];
import [Link];
import [Link];

public class App


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

Session session =

HibernateUtil_Ex1.getSessionFactory().openSession();

Transaction tran = null;

try {
tran = [Link]();
[Link](student);
[Link]();
}
catch (HibernateException e) {
if(tran==null) {
[Link]();
}
[Link]();
}
finally {
[Link]();
}
}
}

You might also like