-->
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.  [ 9 posts ] 
Author Message
 Post subject: Join on 2 tables via Criteria api
PostPosted: Tue Jun 12, 2007 8:08 pm 
Newbie

Joined: Wed Apr 11, 2007 3:16 pm
Posts: 5
Is it possible to do a join on 2 tables that are related by foreign key only using the Criteria api?

I see that the Criteria api allows us to do a join for an association, but if there isn't an association between 2 tables, how do I do a join using this api?

Thanks in advance,
Roopa


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 1:12 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

send more detail about your requirment


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 12:45 pm 
Newbie

Joined: Wed Apr 11, 2007 3:16 pm
Posts: 5
Here is the mapping for the first table

<hibernate-mapping>
<class name="AssignmentQueue" table="ASSIGNMENTQUEUE">
<id name="assignmentQueueId" type="java.lang.Long">
....
</id>
<version column="CHANGESTAMP" name="changeStamp" type="java.lang.Integer"/>
<property name="ouId" type="java.lang.Long">
<column name="ouid" not-null="true" />
</property>
<property name="incomingAddrId" type="java.lang.Long">
<column name="INCOMINGADDRID" not-null="true" />
</property>

The incomingAddrId refers to the id from another table, whose mapping is below:
<hibernate-mapping>
<class name="IncomingAddr" table="INCOMINGADDR">
<id name="incomingAddrId" type="java.lang.Long">
...
</id>
...
<property name="address" type="java.lang.String">
<column name="ADDRESS" not-null="true" />
</property>
...

What I need to do is using the Criteria API, do a join between the two tables so I can get the ouId from AssignmentQueue table and address from the IncomingAddr table.

Can this be done?

Thanks,
Roopa


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 13, 2007 4:13 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
First things first, its not a good idea to have this type of none-referential integrity in your app.

But it can be achieved by this hql :
Code:
select queue,addr from AssignmentQueue queue,IncomingAddr addr where (queue.ouId = addr.incomingAddrId)



Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 1:26 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> but if there isn't an association between 2 tables, how do I do a join using this api?

In 2007 Edition of "Java Persistence With Hibernate" by Christian Bauer & Gavin King,
It is stated that Only joining of associated entities is supported with Criteria api.

This feature is expected ( likely ) in later versions of Hibernate.

But, I don't know if it is available already.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 1:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
jits_1998 wrote:
First things first, its not a good idea to have this type of none-referential integrity in your app.

But it can be achieved by this hql :
Code:
select queue,addr from AssignmentQueue queue,IncomingAddr addr where (queue.ouId = addr.incomingAddrId)



Regards,
Jitendra


It's just a theta style inner join, which is a perfectly good idea in any application and it has also nothing to do with referential integrity. That is the job of foreign key constraints, which obviously are present regardless of what query is executed.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 2:50 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Hi Christian,

According to the hibernate mappings above, there does not appear to be any foreign key relationship in the two tables, unless it is created directly on the db. That is what i meant is not a good idea, having a mismatch in application and db.

Please guide me to correct views on this.

Apart from this, the query will do the job i think ..

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 2:53 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, sorry I missed the mapping. That is indeed an extremely bad idea.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 3:20 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Thanks Christian.

I think I can get the credit now. Can I? I am very eager to open my account of credits :-). Did some interesting work in hibernate and am trying to get involved :-).

cheers!
Jitendra


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.