Monday 28 May 2012

One-to-Many


One to Many UniDirectional


example application on one-many uni-directional Association Mapping based on User and phonenumbers information

Step:1
Tables in DB

user_table

create table user_table(user_id number primary key, first_name varchar2(10));

create table Phone_numbers(number_type varchar2(10),phone number(10),unid number references user_table(user_id));

Step:2(POJO classes)
User.java

public class User {
private long id;
private String name;
private Set<Phones> phones;
Setter and Getter Methods;
}

Phones.java


public class Phones {
private String numberType;
private long phone;
private long id;

Setter and Getter Methods;
}

Step3:hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system123</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<mapping resource="User.hbm.xml"/>
<mapping resource="phone.hbm.xml"/>
</session-factory>
</hibernate-configuration>



Step4:(Mapping Files)

phone.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="Phones" table="phone_numbers">
<id name="phone" column="phone"></id>
<property name="numberType" column="number_type"></property>
<property name="id" column="unid" insert="false" update="false"></property>
</class>
</hibernate-mapping>

user.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="User" table="User_table">
<id name="id" column="user_id"></id>
<property name="name" column="first_name"></property>
<set name="phones" table="phone_numbers" cascade="all">
<key column="unid"></key>
<one-to-many class="Phones"/>
</set>
</class>
</hibernate-mapping>

Step5:(Client Application)


import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Testclient {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session ses=new Configuration().configure().buildSessionFactory().openSession();
Transaction t=ses.beginTransaction();
User u1=new User();
u1.setId(101);
u1.setName("aalala");
Phones p1=new Phones();
p1.setId(111);
p1.setNumberType("off");
p1.setPhone(6543);

Phones p2=new Phones();
p2.setId(222);
p2.setNumberType("res");
p2.setPhone(9963);

Set<Phones> s=new HashSet<Phones>();
s.add(p1);
s.add(p2);

u1.setPhones(s);

ses.save(u1);
t.commit();
System.out.println("finished.....");
}
}





One to Many Bi-Directional

Developing one-to-many bi-directional association based application based on using user,phonenumber details.

Steps:
Database tables:
same as previous

hibernate.cfg.xml(same as above)

User.java(same as above)

PhoneNumber.java.


public class PhoneNumber implements Serializable{
private String numberType;
private long phone;
private long id;
private User parent;
Setter and getter Methods;
}


User.hbm.xml
Same as above

phonenumber.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="PhoneNumber" table="Phone_numbers">
<id name="phone" column="PHONE"></id>
<property name="numberType" column="number_type"></property>
<property name="id" column="unid" insert="false" update="false"></property>
<many-to-one name="parent" class="User" column="unid" cascade="all"></many-to-one>
</class>
</hibernate-mapping>

TestClient.java
(same as above application)

Note: In this example we can access Parent data by using Child table and vice versa. So this is called Bi-Directional Mapping


1 comment:

  1. hello sir/madam
    i read your post interesting and informative. i am doing research on bloggers who use effectively blog for disseminate information.i glad if u wish to participate in my research. if you are interested please contact me through mail. thank u

    ReplyDelete