Hibernate First Application Using Mapping File
Steps to create Hibernate First Application:
===================================================
1. Create Dynamic Web Project
2. Add HB Jars in lib folder
3. Create Bean/POJO class
4. Create Hibernate Configuration File
5. Create Mapping File
6. Create Test class for test the functionality
1. Create Dynamic Web Project
Steps to create Hibernate First Application:
===================================================
1. Create Dynamic Web Project
2. Add HB Jars in lib folder
3. Create Bean/POJO class
4. Create Hibernate Configuration File
5. Create Mapping File
6. Create Test class for test the functionality
1. Create Dynamic Web Project
2. Add HB Jars in lib folder:
-> Download all the jars file https://drive.google.com/file/d/1LRzOK-L6hDRip3LJcDw112pZUA5qft6d/view?usp=sharing
--> Extract all jars files
--> Copy all jars files
--> Paste in lib folder
NOTE - Also add the DATABASE Jars (According to DB what you are using)
After paste lib folder will look like this:
3. Create package
goto ->Java Resource->src. Then right click on src folder:
4. In my case I create package name "bean" - You can take any package name (But it should be valid)
5. Create bean class
package bean;
public class Student {
private int roll;
private String name;
private String course;
private float fees;
private String contact;
public Student(int roll, String name, String course, float fees) {
this.roll=roll;
this.name=name;
this.course=course;
this.fees=fees;
}
public Student(int roll, String name, String course, float fees, String conact) {
this(roll, name, course, fees);
this.contact=contact;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public Student() {}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public float getFees() {
return fees;
}
public void setFees(float fees) {
this.fees = fees;
}
}
6. Create Hibernate Configuration File:
Path - You can save configuration file anywhere in the project but I would recommended to save in src folder by "hibernate.cfg.xml" name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bs</property>
<property name="connection.username">root</property>
<property name="connection.password">7901</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
NOTE - We will discuss more about configuration file in next blog.
7. Create Mapping File
Path - You can save mapping file anywhere in the project but I would recommended to save in src folder by "Bean_Class_Name.hbm.xml" name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="bean.Student" table="studentdb2">
<id name="roll" column="s_roll">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="course"/>
<property name="fees"/>
<property name="contact"/>
</class>
</hibernate-mapping>
NOTE - We will discuss more about mapping file in next blog.
7. Create Test class - to test the functionality
package test; //Make sure you will create test package
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import bean.Student;
public class Test {
public static void main(String[] args) {
//1. create Configuration class object
Configuration cfg = new Configuration();
//2. config the configuration file
cfg.configure("hibernate.cfg.xml");
//3. create SessionFactory object
SessionFactory sf=cfg.buildSessionFactory();
//4. Create Session Object
Session ses=sf.openSession();
//4. if you wand to DDL operation the we need to start the TX
Transaction tx=ses.beginTransaction();
Student st=new Student();
st.setCourse("BCA");
st.setName("Bhupender");
st.setRoll(7904);
st.setFees(23000);
st.setContact("9891297115");
ses.persist(st);
tx.commit();
ses.close();
try {
Thread.sleep(20*1000);
}
catch(Exception e) {}
sf.close();
System.out.println("Inserted!!!");
}
}
8. Run the file
After run you will see record will insert in the database table.
No comments:
Post a Comment