Tuesday, 17 April 2018

Hibenate Example using xml mapping


Hibernate Example
=============================================================================================
//Student.java
//Student.hbm.xml
//hibernate.cfg.xml
StudentSaveData.java
==============================================================================================
step:--
Create the java project
Add jar files for hibernate
Create the Persistent class
Create the mapping file for Persistent class
Create the Configuration file
Create the class that retrieves or stores the persistent object
Run the application
==========================================================================================
jar file required........
antlr-2.7.5H3.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
postgresql-9.3-1103.jdbc3.jar

===============================================================================================
package govind;

public class Student {

private int sid;
private String sname;
private int sage;
private String saddress;
private String srollno;
private float smarks;
private float spercentage;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String getSrollno() {
return srollno;
}
public void setSrollno(String srollno) {
this.srollno = srollno;
}
public float getSmarks() {
return smarks;
}
public void setSmarks(float smarks) {
this.smarks = smarks;
}
public float getSpercentage() {
return spercentage;
}
public void setSpercentage(float spercentage) {
this.spercentage = spercentage;
}

}

==============================================================================================
//Student.hbm.xml
<?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="govind.Student" table="studenthibernate" >
<id name="sid" column="studentid" >
<generator class="assigned" />
</id>

<property name="sname" length= "50" column="studentname"/>
<property name="sage"  length= "3" column="studentage" />
<property name="saddress" length= "50" column="studentaddress"/>
<property name="srollno"  length= "20" column="studentrollno" />
<property name="smarks" length= "5" column="studentmarks"/>
<property name="spercentage"  length= "5" column="studentpercentage" />
</class>

</hibernate-mapping>
==============================================================================================
//hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/java</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">manager</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">30</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
       
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
       
        <property name="hibernate.hbm2ddl.auto">update</property>
       
        <mapping resource="govind/Student.hbm.xml"/>
       
    </session-factory>

</hibernate-configuration>

================================================================================================

package govind;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class StudentSaveData {
public static void main(String[] args) {
Configuration c=new Configuration();
c.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = c.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Student s1=new Student();
s1.setSid(6);
s1.setSname("rishabh");
s1.setSage(25);
s1.setSaddress("hanumannagar");
s1.setSrollno("0105cs13mt08");
s1.setSmarks(500.00f);
s1.setSpercentage(100.00f);
session.persist(s1);
tx.commit();
session.close();
System.out.println("Save Data Successfully");
}
}
================================================================================================

Hibernate: insert into studenthibernate (studentname, studentage, studentaddress, studentrollno, studentmarks, studentpercentage, studentid) values (?, ?, ?, ?, ?, ?, ?)
 Save Data successfully
==============================================================================================

No comments:

Post a Comment