Hi Friends Today i am sharing a basic of Hibernate, How to Store a Student Object In MySql DB Using Hibernate.
I am not explaining so much, follow the steps or copy paste code :P .
Let's Start : -
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">
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test_db?autoReconnect=true&useSSL=false</property>
<property name="connection.user">root</property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true </property>
<property name="dialet">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<!-- <mapping resource="student.hbm.xml" /> -->
<!-- I am not using here student.hbm.xml file so i commented this, if you want to use you can . I am using annotation class so i declared as below. If you want to use hbm file uncomment the above line -->
<mapping class="classes.StudentPojo"/>
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
student.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<class name="StudentPojo" table="STUDENTS">
<id name="id" column="SId" >
<generator class="assigned" />
</id>
<property name="name" column='SName'/>
<property name="mobile"/>
</class>
</hibernate-mapping>
Now create a pojo class :
package classes;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Student")
public class StudentPojo {
@Column(name = "SName")
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Sid")
private int id;
private int mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
}
At last create one class using main method to save Pojo object to MySql DB :
package classes;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SaveStudent {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
StudentPojo p=new StudentPojo();
//p.setId(1);
p.setName("ABC");
p.setMobile(1444456724);
StudentPojo p1=new StudentPojo();
//p1.setId(103);
p1.setName("ABCD");
p1.setMobile(1444346724);
StudentPojo p2=new StudentPojo();
//p2.setId(104);
p2.setName("ABCDD");
p2.setMobile(1454456724);
StudentPojo p3=new StudentPojo();
//p3.setId(105);
p3.setName("ABCDS");
p3.setMobile(1448456724);
Transaction tx = session.beginTransaction();
session.save(p);
session.save(p1);
session.save(p2);
session.save(p3);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
Thats all. save all these file and run :)
I am not explaining so much, follow the steps or copy paste code :P .
Let's Start : -
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">
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test_db?autoReconnect=true&useSSL=false</property>
<property name="connection.user">root</property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true </property>
<property name="dialet">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<!-- <mapping resource="student.hbm.xml" /> -->
<!-- I am not using here student.hbm.xml file so i commented this, if you want to use you can . I am using annotation class so i declared as below. If you want to use hbm file uncomment the above line -->
<mapping class="classes.StudentPojo"/>
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
student.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<class name="StudentPojo" table="STUDENTS">
<id name="id" column="SId" >
<generator class="assigned" />
</id>
<property name="name" column='SName'/>
<property name="mobile"/>
</class>
</hibernate-mapping>
Now create a pojo class :
package classes;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Student")
public class StudentPojo {
@Column(name = "SName")
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Sid")
private int id;
private int mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
}
At last create one class using main method to save Pojo object to MySql DB :
package classes;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SaveStudent {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
StudentPojo p=new StudentPojo();
//p.setId(1);
p.setName("ABC");
p.setMobile(1444456724);
StudentPojo p1=new StudentPojo();
//p1.setId(103);
p1.setName("ABCD");
p1.setMobile(1444346724);
StudentPojo p2=new StudentPojo();
//p2.setId(104);
p2.setName("ABCDD");
p2.setMobile(1454456724);
StudentPojo p3=new StudentPojo();
//p3.setId(105);
p3.setName("ABCDS");
p3.setMobile(1448456724);
Transaction tx = session.beginTransaction();
session.save(p);
session.save(p1);
session.save(p2);
session.save(p3);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
Thats all. save all these file and run :)
Tags:
Dev