-->
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.  [ 2 posts ] 
Author Message
 Post subject: Join with a where clause?
PostPosted: Tue Apr 05, 2011 4:07 pm 
Newbie

Joined: Tue Mar 08, 2011 3:55 pm
Posts: 6
Id like to configure a join between 2 tables, however the join should contain an additional where clause similar to how a bag can be configured. Is this possible?

Id like to do something like the following:

<join table="ADDRESS" where="OUTCOME_ID <> 'O'">
<key column="ADDRESS_ID"/>
</join>

Unfortunately I have no control over the database schema so cannot make changes to the table structure.


Top
 Profile  
 
 Post subject: Re: Join with a where clause?
PostPosted: Wed Apr 06, 2011 2:29 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
Hey Try this ..


Query query = session.createQuery("from Admin admin inner join Customer cust where cust.Name ='Niks'");
query.addEntity(Admin.class);
query.setInteger("username",'"niks');
List =query.list();

This is an native SQL query so you can run it in MySQL query browser for example. You don't use the objects that wrap the tables in the query so any column names are actual table column names and not the names used for the columns in the object. eg cust_id instead of custId. You must define the class to map result to with addEntity. You can define multiple classes as well.

This works like a charm. It is better for maintainability to define this as a named query in the hbm.xml file

_________________
Thanks
Niki


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