Suppose there are two classes Employee and Address. To apply the one-to-one relationship from Employee object to multiple Address objects.
- Create the Project directory structure.
- Create the Persistence classes.
Employee.java
[java]package com.itoolsinfo;
import java.util.Set;
public class Employee
{
private int id;
private String name;
private Set address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getAddress() {
return address;
}
public void setAddress(Set address) {
this.address = address;
}
}
[/java]
Here the developer created the class
Employee with the variables name and address, set and get methods are used on the variables. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.
Address.java
[java]package com.itoolsinfo;
public class Address
{
private int streetNumber;
public int getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(int streetNumber) {
this.streetNumber = streetNumber;
}
private String city, village, street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getVillage() {
return village;
}
public void setVillage(String village) {
this.village = village;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
[/java]
Here the developer created the class
Address with their variables such as streetNumber, city, village. Set and Get operations were performed on this variables.
- Map all POJO classes in mapping file that is employee.hbm.xml.
[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.itoolsinfo.Employee” table=”employee5”>
<id name=”id” column=”id”>
<generator class=”increment”></generator>
</id>
<property name=”name” length=”14”/>
<set name=”address” cascade=”all”>
<key column=”eid”/>
<one-to-many class=”com.itoolsinfo.Address”/>
</set>
</class>
<class name=”com.itoolsinfo.Address”table=”address8” >
<id name=”streetNumber”>
</id>
<property name=”village” length=”14”/>
<property name=”street”length=”14”/>
<property name=”city” length=”14”></property>
</class>
</hibernate-mapping>
[/xml]
- Configure the mapping file in Configuration file.
hibernate.cfg.xml
[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>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[/xml]
Properties |
Description |
hibernate.connection.driver_class |
The JDBC driver class. |
hibernate.dialect |
This property makes Hibernate generate the suitable SQL for the picked database. |
hibernate.connection.url |
The JDBC URL to the database instance. |
hibernate.connection.username |
The database username. |
hibernate.connection.password |
The database password. |
hibernate.connection.pool_size |
Limits the number of connections waiting in the Hibernate database connection pool. |
hibernate.connection.autocommit |
Allows autocommit mode to be used for the JDBC connection. |
- Create the SPLessons class and store the POJO class objects to perform database operations.
SPLessons.java
[java]package com.itoolsinfo;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SPLessons
{
public static void main(String args[])
{
SessionFactory factory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session=factory.openSession();
// To insert parent Employee class property values.
Employee employee=new Employee();
employee.setId(2);
employee.setName(" jhosf " );
// To insert multiple Address class object values.
Address address= new Address();
address.setStreetNumber(330);
address.setStreet("Hariharapuram");
address.setVillage("BNReddy nagar");
address.setCity("LB nagar");
Address address1= new Address();
address1.setStreetNumber(314);
address1.setStreet("yousufguda");
address1.setVillage("Ameerpet");
address1.setCity("Hyderabad");
// To add the address class objects in collection variable.
Set
<Address> set=new HashSet
<Address>();
set.add(address);
set.add(address1);
// To set the collection variable to Employee class object.
employee.setAddress(set);
Transaction transaction=session.beginTransaction();
session.save(employee);
transaction.commit();
session.close();
}
}
[/java]
Here the developer created the class SPLessons to perform database operations as mentioned in the comments of the code.
- See the output in command prompt.
- See the output in database table using following command.
[sql]select * from employee;[/sql]
[sql]select * from address;[/sql]