-->
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.  [ 4 posts ] 
Author Message
 Post subject: Composite-id unexpected Criteria behavior
PostPosted: Thu Aug 04, 2005 11:05 pm 
Newbie

Joined: Thu Dec 30, 2004 3:51 pm
Posts: 14
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.0

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">
<hibernate-mapping>
<class
name="com.domain.model.Sample"
table="SAMPLE">

<composite-id>
<key-property name="id" column="ID" />
<key-property name="sequence" column="SEQ" />
</composite-id>

<property name="field1" type="java.lang.String">
<column name="FIELD1" sql-type="char(6)" />
</property>

<property name="field2" type="java.lang.String">
<column name="FIELD2" sql-type="char(30)" />
</property>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Criteria criteria = session.createCriteria(Sample.class);
criteria.add(Restrictions.eq("id", id));
List list = criteria.list();

Full stack trace of any exception that occurs:
org.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of com.domain.model.Sample.?
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:42)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:257)
at org.hibernate.type.ComponentType.nullSafeGetValues(ComponentType.java:230)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:188)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1042)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1104)
at org.hibernate.loader.Loader.doQuery(Loader.java:365)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.doList(Loader.java:1515)
at org.hibernate.loader.Loader.list(Loader.java:1498)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1253)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)


Name and version of the database you are using:
DB2 UDB for AS/400
05.03.0000 V5R3m0
AS/400 Toolbox for Java JDBC Driver
6.0
The generated SQL (show_sql=true):
select this_.ID as ID0_, this_.SEQ as SEQ0_, this_.FIELD1 as FIELD1_0_, this_.FIELD2 as FIELD2_0_ from SAMPLE this_ where (this_.ID=? and this_.SEQ=?)

Debug level Hibernate log excerpt:

------------------

fairly straight up query expected.
i want to get a list of all Sample based on ID field.
as you'd suspect from the mappings, ID isn't unique, but is a composite along with SEQ.

although i do not specify anything for field SEQ in the criteria, the SQL output adds this_.SEQ=? anyways.

this behavior seems unexpected to me. if i changed the criteria restriction to:
criteria.add(Restrictions.eq("field1", something));
it generates an expected SQL. it only does this when doing a Restriction on a partial composite-id.

is there a setting in the criteria i should be adding?
i already know i can work around this using an HQL query, but would rather have a nice clean mapping whereever possible.

thanks
Fid


Top
 Profile  
 
 Post subject: composite id syntax
PostPosted: Thu Aug 04, 2005 11:56 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suggest trying a bit sifferent syntax:
criteria.add(Restrictions.eq("id.id", id));


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 12:17 am 
Newbie

Joined: Thu Dec 30, 2004 3:51 pm
Posts: 14
wow, that seemed to actually do the trick
i am afraid to ask why though!

thanks kgignatyev
+credits


Top
 Profile  
 
 Post subject: explanation
PostPosted: Fri Aug 05, 2005 1:01 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
This is why: composite id is a "virtual property" with name 'id', so when we need to address part of the identifier we need to use syntax like id.id or id.sequence in your case.


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