Basic Hibernate Mapping:
- One to one mapping
One to one mapping is performed by one-to-one element. No foreign key is created in primary table.
Let us see one simple example, in which we are using bidirectional association.
There are two persistent classes Student.java & Address.java. Student class contain Address class reference and vice versa
Student.java
package com.hibernateExample;
public class Student {
private int studentId;
private String name,email;
private Address address;
}
Address.java
package com.hibernateExample;
public class Address {
private int addressId;
private String addressLine1,city,state,country;
private int pincode;
private Student student;
There are two mapping files for the persistent classes namely, student.hbm.xml & address.hbm.xml
In the mapping files, we are using one-to-one element.
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="com.hibernateExample.Student" table="student">
<id name="studentId">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<property name="email"></property>
<one-to-one name="address" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
address.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="com.hibernateExample.Address" table="address">
<id name="addressId">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<property name="addressLine1"></property>
<property name="city"></property>
<property name="state"></property>
<property name="country"></property>
<one-to-one name="student"></one-to-one>
</class>
</hibernate-mapping>
A configuration file named hibernate.cfg.xml contains information about the database and mapping file.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="student.hbm.xml"/>
<mapping resource="address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
To store the data :
..........................................
.........................................
Student st1=new Student();
st1.setName("RaviShekar");
st1.setEmail("ravi12@gmail.com");
Address address1=new Address();
address1.setAddressLine1("A-22,vikas nagar");
address1.setCity("Ghaziabad");
address1.setState("UP");
address1.setCountry("India");
address1.setPincode(201301);
st1.setAddress(address1);
address1.setStudent(st1);
session.persist(st1);
...............................................
................................................
To fetch the data :
...........................................
........................................
Query query=session.createQuery("from Student s");
List list=query.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
Student stu=itr.next();
Address address=stu.getAddress();
.......................................
...............................................
Hope it helps someone!
0 Comment(s)