-->
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.  [ 7 posts ] 
Author Message
 Post subject: Foreign key / one to many relationship at query time
PostPosted: Tue Aug 10, 2004 8:40 am 
Beginner
Beginner

Joined: Tue Jul 27, 2004 2:50 pm
Posts: 23
I am using the following version of hibernate and oracle:

Hibernate version: 2.1

Name and version of the database you are using: Oracle 9i

My question is that:

I have two tables T and G. G has a T.id as a foreign key in it. We want this to be transparent to the users of the APIs we are developing. Therefore, our G Java class does not include a T.id attribute.

Furthermore, G could be associated with many T's, but each T is only associated with one G.

We are interested in forcing this foreign key relationship (or one-to-many relationship) when only querying the system.

All the examples I have seen so far either have G include T.id in its class definition, which makes life easier, but we cannot do it in our case or have
G include a set of T's in its class definition, but this is not a valid option in our case either.

Any other solutions to this problem using Hibernate?

Thanks,
Payam.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 8:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you don't have any associations in your classes, you should probably not use a ORM, you are not really utilizing its features.

Besides, you can do where-clause joins in HQL, too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 8:54 am 
Beginner
Beginner

Joined: Tue Jul 27, 2004 2:50 pm
Posts: 23
Thanks for your reply. I tried to do the HQL join, but was not sure how to set it up.

This is the query I am interested in doing:

Code:
select *
from g, t
where g.tid = t.id
and t.id = 6


Here is my HQL query try:

[code]
net.sf.hibernate.Criteria criteria = session.
createCriteria(G.class).
setFetchMode("t", FetchMode.LAZY).
add(net.sf.hibernate.expression.Expression.eq("id", new Long(6)));
java.util.List c = criteria.list();
[code]

And I do not have anything specific in G.hbm.xml and T.hbm.xml mapping files that would refer to this one-to-many relationship or foreign key constraint.

Any hints/help on how to fix this HQL join would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 8:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
from g, t
where g.tid = t.id
and t.id = 6

if your properties are named tid and id

don't use the Criteria api


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 9:54 am 
Beginner
Beginner

Joined: Tue Jul 27, 2004 2:50 pm
Posts: 23
Well, here is the other thing. I have to use the HQL apis per our requirements. Any suggestions for that?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 1:37 pm 
Beginner
Beginner

Joined: Tue Jul 27, 2004 2:50 pm
Posts: 23
Any suggestions for this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 5:15 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
You're example wasn't HQL - the Criteria API and HQL are two separate ways of carrying out querying, which is why they are in two separate chapters in the docs.

The chapter in the docs for HQL is: http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html

The chapter in the docs for the Criteria API is: http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html

Are you saying you're limited to using the Criteria API in your application? If you are, you are going to run into problems running anything more than fairly basic queries.


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

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.