One to One UniDirectional
One to one means if one parent record is pointing to one child record then it is called One-One Association Mapping. Here One-One UniDirectional is possible and Bi-directional is also Possible.
if we access child record with help of parent record and if reverse is not also possible then it is called One-One Uni-Directional.
if we access child record with help of parent record and if reverse is also possible then it is called One-One Bi-Directional.
Example Application for One-One Uni Directional
This Example describes student record is mapped Library table
structure for student table(std)
create table std
(
stdno number(3) primary key,
stdname varchar2(10)
);
Structure for Library table(lib)
Create table lib
(
id number(3) references std(stdno),
jdate date
);
POJO class for STD
public class Std {
private int id;
private String name;
private Lib l;
Setter and Getter Methods
}
POJO class for Lib
public class Lib implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private Date jdate;
Setter and Getter Methods
}
Mapping file for std(std.hbm.xml)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Std" table="std">
<id name="id" column="stdno">
<generator class="increment"></generator>
</id>
<property name="name" type="string" length="10"></property>
<one-to-one name="l" class="Lib"></one-to-one>
</class>
</hibernate-mapping>
Mapping file for lib(lib.hbm.xml)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Lib" table="lib">
<id name="id" column="id">
<generator class="increment">
</generator>
</id>
<property name="jdate" column="jdate" type="date" length="20"></property>
</class>
</hibernate-mapping>
Client application
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Std obj=new Std();
obj.setName("abc");
Lib l=new Lib();
l.setJdate(new Date());
obj.setL(l);
s.save(obj);
s.save(l);
t.commit();
System.out.println("finished....");
}
}
To Download Application
One to one means if one parent record is pointing to one child record then it is called One-One Association Mapping. Here One-One UniDirectional is possible and Bi-directional is also Possible.
if we access child record with help of parent record and if reverse is not also possible then it is called One-One Uni-Directional.
if we access child record with help of parent record and if reverse is also possible then it is called One-One Bi-Directional.
Example Application for One-One Uni Directional
This Example describes student record is mapped Library table
structure for student table(std)
create table std
(
stdno number(3) primary key,
stdname varchar2(10)
);
Structure for Library table(lib)
Create table lib
(
id number(3) references std(stdno),
jdate date
);
POJO class for STD
public class Std {
private int id;
private String name;
private Lib l;
Setter and Getter Methods
}
POJO class for Lib
public class Lib implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private Date jdate;
Setter and Getter Methods
}
Mapping file for std(std.hbm.xml)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Std" table="std">
<id name="id" column="stdno">
<generator class="increment"></generator>
</id>
<property name="name" type="string" length="10"></property>
<one-to-one name="l" class="Lib"></one-to-one>
</class>
</hibernate-mapping>
Mapping file for lib(lib.hbm.xml)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Lib" table="lib">
<id name="id" column="id">
<generator class="increment">
</generator>
</id>
<property name="jdate" column="jdate" type="date" length="20"></property>
</class>
</hibernate-mapping>
Client application
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Std obj=new Std();
obj.setName("abc");
Lib l=new Lib();
l.setJdate(new Date());
obj.setL(l);
s.save(obj);
s.save(l);
t.commit();
System.out.println("finished....");
}
}
To Download Application
One - One Bi-Directional
if we access child record with help of parent record and if reverse is also possible then it is called One-One Bi-Directional.
This Application is same as above application but here we need to modify some resources
1.Lib.java
2.lib.hbm.java
3.client.java
To Download Application
No comments:
Post a Comment