aspose file tools*
The moose likes Object Relational Mapping and the fly likes One to One mapping in Hibernate 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "One to One mapping in Hibernate 3" Watch "One to One mapping in Hibernate 3" New topic
Author

One to One mapping in Hibernate 3

Murali Charan
Greenhorn

Joined: May 04, 2007
Posts: 11
Hi,

I am trying to write a sample for One To One mapping in Hibernate 3.

Following is the scenario.

I have an Employee object and a Parking Space Object. The have a one to one relation i.e., an employee will always be associated to only one parking space, that parking space should not be associated to any other employee. I dont have this restriction in the Database. I want to have it only in my application.

Following is my employee code.

package com.sample.employee;

public class Employee
{
private long employeeId;
private String employeeName;
private long salary;
private ParkingSpace parkingSpace;

public void setEmployeeId(long employeeId)
{
this.employeeId = employeeId;
}

public long getEmployeeId()
{
return this.employeeId;
}

public void setEmployeeName(String employeeName)
{
this.employeeName = employeeName;
}

public String getEmployeeName()
{
return this.employeeName;
}

public void setSalary(long salary)
{
this.salary = salary;
}

public long getSalary()
{
return this.salary;
}

public void setParkingSpace(ParkingSpace parkingSpace)
{
this.parkingSpace = parkingSpace;
}

public ParkingSpace getParkingSpace()
{
return this.parkingSpace;
}

}

Following is my Parking Space Pojo

package com.sample.employee;

public class ParkingSpace
{
private long parkingSpaceId;
private String lotName;
private Employee employee;

public void setParkingSpaceId(long parkingSpaceId)
{
this.parkingSpaceId = parkingSpaceId;
}

public long getParkingSpaceId()
{
return this.parkingSpaceId;
}

public void setLotName(String lotName)
{
this.lotName = lotName;
}

public String getLotName()
{
return this.lotName;
}

public void setEmployee(Employee employee)
{
this.employee = employee;
}

public Employee getEmployee()
{
return this.employee;
}
}

Following is my Employee HBM file

<?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="com.sample.employee.Employee" table="EMPLOYEE_DETAILS">
<id name="employeeId" type="long" column="EMPLOYEE_ID" >
<generator class="assigned"/>
</id>
<property name="employeeName">
<column name="EMPLOYEE_NAME" />
</property>
<property name="salary">
<column name="SALARY"/>
</property>

<many-to-one name="parkingSpace" class = "com.sample.employee.ParkingSpace" column="PARKING_SPACE_ID" unique="true" />

</class>
</hibernate-mapping>

Note: I did try using <one-to-one name="parkingSpace" class = "com.sample.employee.ParkingSpace" column="PARKING_SPACE_ID" />

When I tried this a mapping exception was thrown saying the one-to-one tag does not have column attribute.

When I removed it and tried the id of the parking space object was not persisted in the Employee table. So, I tried using the <many-to-one> tag, with this tag I was able to get the parking soace id to be persisted in the employee table, but the issue was that the same parking sapce id was persisted for different employees. I thought using the attribute unique="true" would solve the problem, but not be.

Following is my parking space hbm file.

<?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="com.sample.employee.ParkingSpace" table="PARKING_SPACE">
<id name="parkingSpaceId" type="long" column="PARKING_SPACE_ID" >
<generator class="native"/>
</id>

<property name="lotName">
<column name="LOT_NAME" />
</property>
<one-to-one name="employee" class="com.sample.employee.Employee" property-ref="parkingSpace"/>

</class>
</hibernate-mapping>

Using this code i am not getting any exception as such, but I am not able to enforce one to one mapping i.e., same parking space id was allowed to be used by multiple employees.

If in case I could use the propert-ref attribute please let me know how to use that attribute in this sample.

Any soltion in this issue would be very helpful.

Thanks in Advance,

Murali.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Here is a link to the Hibernate documentation for implementing one-to-one mapping. There are actually a couple of ways of doing it. And one is like what you have and unique="true" is part of it.

http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-onetoone

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Murali Charan
Greenhorn

Joined: May 04, 2007
Posts: 11
Thanks Mark, for putting me out of my misery.

That was really helpful.

Murali.
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
Have a look at this
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp

Rashid
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Rashid Darvesh:
Have a look at this
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp

Rashid


Rashid, please be careful about promoting your own tutorial. It looks like you just want to promote your page rather than helping people. If you know that answer then post the answer instead of the link. I would say your best bet it to include your tutorial in your Signature.

Mark
Vasan Varadarajan
Greenhorn

Joined: May 23, 2008
Posts: 3
Actually Rashid's post worked - There is a fine detail (some attribute of hibernate, or the java test code) in the link, that fixed the problem. I tried to go back to my old code to recreate the issue, and be more specific in this post, but could not - sorry.
Thanks Rashid..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One to One mapping in Hibernate 3
 
Similar Threads
Need to know Java logic to form a setter method for XML property
how to use one-to-one association
Hibernate Table per concrete class is throwing org.hibernate.NonUniqueObjectException:
Hibernate exception for one-to-one mapping
Different Mapping Types in Hibernate