I'm trying to join two tables, Project and Owner, that have a many-to-one relationship (Project has one Owner. An Owner has many Projects). The column that joins these two tables is unique, but not a primary key. In effect, I need to do the equivalent of the SQL query:
SELECT ...
FROM PROJECT P
LEFT JOIN OWNER O
ON P.ownername = O.ownername
WHERE P.id = ?
In Java, I want to be able to access the Owner via the Project.
Project p = getProjectFromDb( 1234 );
Owner o = p.getOwner();
There are constraints: I need to use a Criteria query, and I can't change the DB schema (it's a legacy schema). I can change the classes and mappings in whatever way I need.
The Hibernate docs give plenty of examples when the join criteria involves a proper foreign key relationship, but I can't seem to find an example for when the join criteria is more complex.
Table definitions:
Project
id - int
ownername - varchar
Owner
id - int
ownername - varchar
Owner.ownername is unique, but there is no constraint on the column.
Hibernate version: 3.X
Name and version of the database you are using: SQL Server 2005
|