Meyyappan has posted 101 posts at DZone. View Full User Profile

Hibernate Mapping One-to-One using Annotations Tutorial

06.14.2012
| 58617 views |
  • submit to reddit

In this example you will learn how to map one-to-one relationship using Hibernate Annotations. Consider the following relationship between Student and Address entity.

According to the relationship each student should have a unique address.

To create this relationship you need to have a STUDENT and ADDRESS table. The relational model is shown below.

To create the STUDENT and ADDRESS tables you need to create the following Java classes with hibernate annotations.

Student class is used to create the STUDENT table.

package com.vaannila.student;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student {

	private long studentId;
	private String studentName;
	private Address studentAddress;

	public Student() {
	}

	public Student(String studentName, Address studentAddress) {
		this.studentName = studentName;
		this.studentAddress = studentAddress;
	}

	@Id
	@GeneratedValue
	@Column(name = "STUDENT_ID")
	public long getStudentId() {
		return this.studentId;
	}

	public void setStudentId(long studentId) {
		this.studentId = studentId;
	}

	@Column(name = "STUDENT_NAME", nullable = false, length = 100)
	public String getStudentName() {
		return this.studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	@OneToOne(cascade = CascadeType.ALL)
	public Address getStudentAddress() {
		return this.studentAddress;
	}

	public void setStudentAddress(Address studentAddress) {
		this.studentAddress = studentAddress;
	}

}

The @OneToOne annotation is used to create the one-to-one relationship between the Student and Address entities.

Address class is used to create the ADDRESS table.

package com.vaannila.student;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ADDRESS")
public class Address {

	private long addressId;
	private String street;
	private String city;
	private String state;
	private String zipcode;

	public Address() {
	}

	public Address(String street, String city, String state, String zipcode) {
		this.street = street;
		this.city = city;
		this.state = state;
		this.zipcode = zipcode;
	}

	@Id
	@GeneratedValue
	@Column(name = "ADDRESS_ID")
	public long getAddressId() {
		return this.addressId;
	}

	public void setAddressId(long addressId) {
		this.addressId = addressId;
	}
	
	@Column(name = "ADDRESS_STREET", nullable = false, length=250)
	public String getStreet() {
		return this.street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	@Column(name = "ADDRESS_CITY", nullable = false, length=50)
	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Column(name = "ADDRESS_STATE", nullable = false, length=50)
	public String getState() {
		return this.state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10)
	public String getZipcode() {
		return this.zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

}

Now create the hibernate configuration file with the Student and Address class mapping.

<?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="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhost</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.vaannila.student.Student" />
        <mapping class="com.vaannila.student.Address" />
    </session-factory>
</hibernate-configuration>

Create the Main class to run the example.

package com.vaannila.student;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.util.HibernateUtil;

public class Main {

	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Address address1 = new Address("OMR Road", "Chennai", "TN", "600097");
			Address address2 = new Address("Ring Road", "Banglore", "Karnataka", "560000");
			Student student1 = new Student("Eswar", address1);
			Student student2 = new Student("Joe", address2);
			session.save(student1);
			session.save(student2);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

}

On executing the Main class you will see the following output.

The Student table has two records.

The Address table has two record.

Each student record points to a different address record, this illustrates the one-to-one mapping.

The folder structure of the example is shown below.

You can download the source code of this example here.

Source :Download
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Adrien Be replied on Thu, 2012/10/04 - 11:45am

First: thanks for the article.

Second: I had a couple of errors when running an example inspired by this article. Hibernate was generating some funny query that declared an error like "MySQLSyntaxErrorException: Unknown column ' ____ ' in 'field list'". The reason seems to be that it takes the name of the field containing the other table (studentAddress in your case) and append it the name of its primary key (addressId in your case) to get the name of the foreign in your main table (students table in your case). anyhow, took me some time to figure that one out and wondered why I had this behavior. Maybe because I'm on a different version of hibernate?? (3.6.10.Final)

Adrien Be replied on Fri, 2012/10/05 - 7:29am

Found how to fix that behavior: use the annotation @JoinColumn such as @JoinColumn(name="hereYouPutTheNameOfTheColumnMappingWithTheIdOfTheOtherTable").

In my case: 

@Entity

@Table(name="PROFILE_DETAILS")

public class ProfileDetails {

    @Id

    @GeneratedValue

    @Column(name="ID")

    private Integer id;

 

    @Column(name="FIRSTNAME", nullable = false, length = 70)

    private String firstname;

 

    @Column(name="LASTNAME", nullable = false, length = 100)

    private String lastname;

 

    @OneToOne(cascade = CascadeType.ALL)

    @JoinColumn(name = "PROFILE_PIC_ID")

    private ProfilePic profile_Pic; 

 

    // Getters & Setters ...

 

@Entity

@Table(name = "PROFILE_PICS")

public class ProfilePic {

    @Id

    @GeneratedValue

    @Column(name="ID")

    private Integer id;

 

    @Column(name="NAME", length = 50)

    private String name;

 

    // and so on... 

 

And my tables:

CREATE TABLE PROFILE_PICS

(

    id             INT PRIMARY KEY AUTO_INCREMENT,

    name           VARCHAR(30),

    ...

);

 

CREATE TABLE PROFILE_DETAILS

(

    id              INT PRIMARY KEY AUTO_INCREMENT,

    firstname    VARCHAR(30),

    lastname    VARCHAR(30),

profile_pic_id       INT,

INDEX par_ind (profile_pic_id), FOREIGN KEY (profile_pic_id) REFERENCES PROFILE_PICS(id),

    created     TIMESTAMP DEFAULT NOW()

); 

 

Voila :) 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.