In this post under Hibernate, I will explain how to create a unidirectional one to one association between objects using a mapping file with an example.
For our example, we will create the below two tables.
Data Definition Language
CREATE TABLE `ticket1` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`number` VARCHAR(50) NOT NULL,
`description` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;
CREATE TABLE `person1` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`description` VARCHAR(100) NOT NULL,
`ticket_id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK_person_ticket` (`ticket_id`),
CONSTRAINT `FK_person_ticket` FOREIGN KEY (`ticket_id`) REFERENCES `ticket1` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
According to above sql code, I have created two tables “ticket1” and “person1”. In Object Oriented world, there will be one to one association between “ticket1” and “person1”. In other words one “person1” instance will have one instance of “ticket1”. So to create a one to one association in RDBMS i.e., the navigation from “person1” to “ticket1”, I have added a foreign key “ticket_id” column in “person1” table. This foreign key will refer to primary column in “ticket1” table.
Next we will create java classes for the above tables
Person1
public class Person1 {
private int id;
private String name;
private String description;
private Ticket1 ticket;
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 String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Ticket1 getTicket() {
return ticket;
}
public void setTicket(Ticket1 ticket) {
this.ticket = ticket;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(id).append(":");
sb.append(name).append(":");
sb.append(description).append(":");
sb.append(getTicket());
return sb.toString();
}
}
Ticket1
public class Ticket1 {
private int id;
private String number;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(id).append(":");
sb.append(number).append(":");
sb.append(description);
return sb.toString();
}
}
Since we are creating an unidirectional one to one association meaning the navigation will be from “Person1” to “Ticket1”. In the Person1 class code, we have added “ticket” property of type Ticket1.
Next we add hbm.xml file for the above java classes
Person1.hbm.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5 <hibernate-mapping>
6 <class name="Person1" table="Person1">
7 <id type="int" name="id" column="id">
8 <generator class="native"/>
9 </id>
10 <property name="name" column="name" type="string"/>
11 <property name="description" column="description" type="string"/>
12 <many-to-one name="ticket" class="Ticket1" column="ticket_id" unique="true"/>
13 </class>
14 </hibernate-mapping>
In the above xml file at line 12, I have added many-to-one element with name as “ticket”, class as “Ticket1”, column attribute value equal to foreign key column name in the “person1” table, and unique attribute equals to true.
Ticket1.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Ticket1" table="Ticket1">
<id type="int" name="id" column="id">
<generator class="native"/>
</id>
<property name="number" column="number" type="string"/>
<property name="description" column="description" type="string"/>
</class>
</hibernate-mapping>
Below is hibernate configuration file for your reference
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/examples</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->
<property name="hibernate.show_sql">true</property>
<mapping resource="Person1.hbm.xml"/>
<mapping resource="Ticket1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Below is the main code
Main Code
1 import org.hibernate.Session;
2 import org.hibernate.SessionFactory;
3
4 public class HibernateDemo12 {
5 public static void main(String[] args) {
6 SessionFactory sessionFactory = HibernateUtil.createSessionFactory();
7
8 Person1 person = new Person1();
9 person.setName("Sumanth");
10 person.setDescription("description");
11
12 Ticket1 ticket = new Ticket1();
13 ticket.setNumber("Ticket1");
14 ticket.setDescription("description");
15
16 person.setTicket(ticket);
17
18 Session session = sessionFactory.openSession();
19 session.beginTransaction();
20 session.save(ticket);
21 int id = (Integer)session.save(person);
22 session.getTransaction().commit();
23 session.close();
24
25 person = null;
26
27 session = sessionFactory.openSession();
28 person = session.load(Person1.class, id);
29 System.out.println(person);
30 session.close();
31 HibernateUtil.shutdown();
32 }
33 }
In the main code, at line 8 we create a new instance of Person1.
At line 12, we create a new instance of Ticket1.
At line 16, we set the ticket property of person instance by calling “setTicket” method.
At line 21, we save the person instance, the ticket instance will also be saved.