-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Joining entities on non-primary key columns
PostPosted: Thu Apr 03, 2008 5:07 pm 
Newbie

Joined: Thu Apr 03, 2008 4:52 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.