-->
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.  [ 8 posts ] 
Author Message
 Post subject: Composite key criteria - restriction behavior
PostPosted: Tue Mar 28, 2006 1:22 pm 
Newbie

Joined: Fri Mar 24, 2006 5:44 pm
Posts: 6
Hibernate version: 3.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 10, 2006 6:09:36 PM by Hibernate Tools 3.1.0.beta4 -->
<hibernate-mapping package="com.dowjones.wealthmanager.customer.clientinformation.dataaccess">
<class name="Client" table="TB_CLIENT" schema="CUSTOMER">
<composite-id name="id" class="ClientId">
<key-property name="userId" type="string">
<column name="USER_ID" length="10" />
</key-property>
<key-property name="nameSpace" type="string">
<column name="NAME_SPACE" length="10" />
</key-property>
<key-property name="clientId" type="string">
<column name="CLIENT_ID" length="25" />
</key-property>
</composite-id>
<property name="firstName" type="string">
<column name="FIRST_NAME" length="25" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="LAST_NAME" length="25" not-null="true" />
</property>
<property name="emailIdPrimary" type="string">
<column name="EMAIL_ID_PRIMARY" length="10" not-null="true" />
</property>
<property name="companyName" type="string">
<column name="COMPANY_NAME" length="10" not-null="true" />
</property>
<property name="address1" type="string">
<column name="ADDRESS1" length="10" not-null="true" />
</property>
.....
.....
.....
<set name="tbUserClients" inverse="true">
<key>
<column name="USER_ID" length="10" not-null="true" />
<column name="NAME_SPACE" length="10" not-null="true" />
<column name="CLIENT_ID" length="25" not-null="true" />
</key>
<one-to-many class="UserClient" />
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Criteria criteria = session.createCriteria(Client.class);
criteria.add(Restrictions.eq("id.userId","xyz"));
criteria.add(Restrictions.eq("id.nameSpace","123"));

Name and version of the database you are using:O racle 10g

The generated SQL (show_sql=true):

select

this_.USER_ID as USER1_7_0_, this_.NAME_SPACE as NAME2_7_0_, this_.CLIENT_ID as CLIENT3_7_0_, this_.FIRST_NAME as FIRST4_7_0_, this_.LAST_NAME as LAST5_7_0_, this_.EMAIL_ID_PRIMARY as EMAIL6_7_0_, this_.COMPANY_NAME as COMPANY7_7_0_, this_.ADDRESS1 as ADDRESS8_7_0_, this_.EXTERNAL_ID as EXTERNAL9_7_0_, this_.ADDRESS2 as ADDRESS10_7_0_, this_.CITY as CITY7_0_, this_.STATE as STATE7_0_, this_.ZIP as ZIP7_0_, this_.COUNTRY as COUNTRY7_0_, this_.CONTACT_MAIN as CONTACT15_7_0_, this_.STATE_OTHER as STATE16_7_0_, this_.NOTES_ID as NOTES17_7_0_, this_.PRIORITY as PRIORITY7_0_, this_.PREFIX as PREFIX7_0_, this_.SECOND_ADDRESS1 as SECOND20_7_0_, this_.SUFFIX as SUFFIX7_0_, this_.SECOND_ADDRESS2 as SECOND22_7_0_, this_.NICK_NAME as NICK23_7_0_, this_.SECOND_CITY as SECOND24_7_0_, this_.MIDDLE_NAME as MIDDLE25_7_0_, this_.SECOND_STATE as SECOND26_7_0_, this_.JOB_TITLE as JOB27_7_0_, this_.SECOND_STATE_OTHER as SECOND28_7_0_, this_.HOME_PHONE as HOME29_7_0_, this_.SECOND_ZIP as SECOND30_7_0_, this_.WORK_PHONE as WORK31_7_0_, this_.SECOND_COUNTRY as SECOND32_7_0_, this_.CELL_PHONE as CELL33_7_0_, this_.FAX as FAX7_0_, this_.EMAIL_ID_SECONDARY as EMAIL35_7_0_, this_.OTHER_INFO as OTHER36_7_0_, this_.CREATION_TIME_STAMP as CREATION37_7_0_, this_.CREATE_BY_ID as CREATE38_7_0_, this_.MODIFICATION_TIME_STAMP as MODIFIC39_7_0_, this_.MODIFIED_BY_ID as MODIFIED40_7_0_, this_.WORK_PHONE_EXT as WORK41_7_0_, this_.OTHER_PHONE as OTHER42_7_0_, this_.OTHER_PHONE_EXT as OTHER43_7_0_, this_.CREATED_BY_NS as CREATED44_7_0_, this_.MODIFIED_BY_NS as MODIFIED45_7_0_

from CUSTOMER.TB_CLIENT this_ where this_.USER_ID=? and this_.NAME_SPACE=?


Problem: I am trying to retrieve all clients for a user id and namespace combination but the values that I pass for the columns user id and namespace doesnt seem to form part of the query thats being finally executed.This is happening for any column which forms part of the composite key and on which I want to create a restriction. If i specify a non-composite key column as a Restriction things are working completely fine.

What needs to be done to achieve this functionality where I can specify a "where" clause for a column which forms part of a composite key?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:34 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Normally, I'd say that composite ids and criteria have lots of foibles, but in this case, everything looks correct. The highlighted part of your SQL is correct, isn't it? Don't you want to filter by USER_ID and NAME_SPACE? Or is the bold section not in there, you're highlighting what you want to see but don't?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:38 am 
Newbie

Joined: Fri Mar 24, 2006 5:44 pm
Posts: 6
Yes the highlighted parts are whatever I can already see in console.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:21 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then isn't it working? It's the correct query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:24 am 
Newbie

Joined: Fri Mar 24, 2006 5:44 pm
Posts: 6
If you notice the values that I am specifying for the "user id" and "namespace" column restrictions are not being put into the query generated.The query generated contains "?" instead.
If I specified a value for a non composite key column criteria restriction then the query is generated proper with the value put into the query E.g "where this_.xyx = <given value in the restriction declaration >".
Seems like a Hibernate code bug here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 6:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Those are JDBC positional parameters. They're supposed to be there. If you want to see the values that get bound to them, add this line to your log4j.properties:
Code:
log4j.logger.org.hibernate.type=DEBUG


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:13 pm 
Newbie

Joined: Fri Mar 24, 2006 5:44 pm
Posts: 6
Why is it that when i specify a non-composite key column as the restriction in the Criteria the values specified show up in the generated SQL query,even in that case I should have got JDBC position al "?" coming up isnt it..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:22 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I have no idea. I don't get that. All parameters are always passed in using JDBC positional parameters. Can you show be code that does it, and the resulting SQL?


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