Hi,
I'm trying to create a relationship as below:
Code:
USER:
ID VARCHAR2(32) PRIMARY KEY
ADDRESS:
ID VARCHAR2(32) PRIMARY KEY
USER_ID VARCHAR2(32) FOREIGN KEY REFERENCES USER(ID)
User has a one-to-one relation with Address, and the foreign key is specified at the address table.
With this, I have the following configurations:
Hibernate version: 2.1.8 Mapping documents:
Code:
<class name="User" table="USER">
<id name="id" column="ID">
<generator class="uuid.hex" />
</id>
<one-to-one name="" class="Address" cascade="all" />
</class>
<class name="Address" table="USER">
<id name="id" column="ID">
<generator class="uuid.hex" />
</id>
<many-to-one name="user" class="User" column="USER_ID" not-null="true" />
</class>
Code between sessionFactory.openSession() and session.close():
Code:
doSaveAndRetrieve() {
HibernateSession session = HibernateUtil.getSession();
User user = new User();
HibernateUtil.beginTransaction();
session.save(user);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria(User.class);
criteria.add(Expression.eq("id", user.getId()));
List results = criteria.list();
Iterator it = results.iterator();
while (it.hasNext()) {
user = (User) it.next();
assertNotNull(user.getAddress());
}
}
Name and version of the database you are using: Oracle 8iThe persisting of the Address object is working perfectly fine with this configuration. However, the retrieval of the Address object is not working correctly. The assertNotNull() in above code fails, as Hibernate is unable to get the linked Address object for a User. The query generated by Hibernate is as below:
The generated SQL (show_sql=true):
Code:
select this.ID as ID2_, address1_.ID as ID0_, address1_.USER_ID as USER1_0_ from USER this, ADDRESS address1_ where this.ID=? and this.ID=address1_.ID(+)
This is an incorrect query. The WHERE clause should have been:
where this.ID=? and this.ID=address1_.USER_ID(+)
How can I specify the mapping for such a relationship? There seems to be a restriction on Hibernate that when specifying one-to-one, the foreign key should be specified on the parent table itself to the child table. Is this a limitation of Hibernate?
Thanks,