one to one mapping in hibernate:-------------------------
// Student.java
//Address.java
//student.hbm.xml
//hibernate.cfg.xml
//InsertData.java
//SelectAll.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
===============================================================================================
// Student.java
package govind;
public class Student {
private int sid;
private String sname;
private Address address;
Student()
{
super();
}
Student (int sid,String sname)
{
this.sid=sid;
this.sname=sname;
}
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
=================================================================================================
// Address.java
package govind;
public class Address {
private int sid;
private String flatno,plotno,street,city;
private Student student;
public Address()
{
super();
}
public Address( String flatno, String plotno, String street, String city, Student student) {
super();
this.flatno = flatno;
this.plotno = plotno;
this.street = street;
this.city = city;
this.student = student;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getFlatno() {
return flatno;
}
public void setFlatno(String flatno) {
this.flatno = flatno;
}
public String getPlotno() {
return plotno;
}
public void setPlotno(String plotno) {
this.plotno = plotno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
==============================================================================================
//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="studentinfo">
<id name="sid">
<generator class="sequence">
<param name="sequence">student_seq</param>
</generator>
</id>
<property name="sname"/>
<one-to-one name="address" class="govind.Address"></one-to-one>
</class>
<class name="govind.Address" table="addressinfo">
<id name="sid">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="flatno"/>
<property name="plotno"/>
<property name="street"/>
<property name="city"/>
<one-to-one name="student" class="govind.Student" constrained="true"></one-to-one>
</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>
============================================================================================
//InsertData.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class InsertData {
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(0,"mani");
Address a1=new Address("10","55-a","f-block","patna",s1);
session.save(a1);
session.flush();
tx.commit();
session.close();
System.out.println("Insert Data Successfully");
}
}
=============================================================================================
//SelectAll.java
package govind;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class SelectAll {
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();
Query query = session.createQuery("from Address");
List l = query.list();
System.out.println("--------------address table data----------------");
for(Object o:l)
{
Address ak=(Address)o;
System.out.println(ak.getSid()+"----"+ak.getStreet()+"----"+ak.getPlotno()+"-----"+ak.getFlatno()+"----"+ak.getCity());
}
Query q = session.createQuery("from Student");
List ll = q.list();
System.out.println("--------------student table data----------------");
for(Object o:ll)
{
Student sk=(Student)o;
System.out.println(sk.getSid()+"----"+sk.getSname());
}
//tx.commit();
session.close();
}
}
o/p view:----------------
case:-1
InsertData.java........run
Hibernate: select nextval ('student_seq')
Hibernate: insert into studentinfo (sname, sid) values (?, ?)
Hibernate: insert into addressinfo (flatno, plotno, street, city, sid) values (?, ?, ?, ?, ?)
Insert Data Successfully
SelectAll.java-----------run
Hibernate: select address0_.sid as sid1_, address0_.flatno as flatno1_, address0_.plotno as plotno1_, address0_.street as street1_, address0_.city as city1_ from addressinfo address0_
--------------address table data----------------
4----2-----59-c-----c-block--------Newdelhi
5----5-----59-a-----d-block--------bhopal
6----10-----55-a-----f-block--------patna
7----11-----52-a-----s-block--------up
Hibernate: select student0_.sid as sid0_, student0_.sname as sname0_ from studentinfo student0_
--------------student table data----------------
4----govind
5----ajit
6----mani
7----sanjay
No comments:
Post a Comment