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.