• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

One to One mapping in Hibernate 3

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Murali Charan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark, for putting me out of my misery.

That was really helpful.

Murali.
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp

Rashid
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic