-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapped composite-id: strange behaviour in queries
PostPosted: Tue Mar 07, 2006 12:39 pm 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
Hibernate version: 3.1.x

When I have a mapped composite-id, e.g.:
Code:
    <class name='T1' table='t1'>
        <composite-id mapped='true' class='T1$Id'>
            <key-property name='id'/>
            <key-property name='validFrom' type='imm_timestamp'/>
        </composite-id>
        <property name='t3'/>
    </class>


and I make a HQL query on a part of the composite-id, such as:
Code:
from T1 h where h.id=10 order by h.validFrom


the result is an invalid query:
Quote:
select ... from pms.t1 t10_ where (t10_.id, t10_.valid_from)=10 order by t10_.valid_from


As you see, even though the mapped composite-id class is backed by individual properties on the T1 class (id and validFrom in this case), the query treats them as a single block and won't generate a query on id alone. It always wants to consider id and validFrom together.

It is not uncommon to make a query on just a part of a composite primary key, but it seems impossible in HQL.

Should this be considered a bug or undesired behaviour? Any workarounds? It makes the mapped composite-id kind of useless in my opinion.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 1:13 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
try this

Code:
<class name='T1' table='t1'>
        <composite-id name="tid" mapped='true' class='Tid'>
            <key-property name='id'/>
            <key-property name='validFrom' type='imm_timestamp'/>
        </composite-id>
        <property name='t3'/>
</class>


and query :

Code:
from T1 h where h.tid.id=10 order by h.tid.validFrom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 4:11 pm 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
Alas, that doesn't work:

Quote:
Caused by: org.hibernate.MappingException: cannot combine mapped="true" with specified name


In a way logical, since mapped="true" means that you don't really have a component but just use the class like a kind of overlay.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 4:36 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
remove it and make class


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 8:56 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
snpesnpe wrote:
remove it and make class


I can't make a class since we're mapping the same class twice here. In one case only the id is primary key, in the other case (for historization purposes) we need a composite key containing both id and validFrom.

The fact that a mapped composite-id works like a kind of overlay appealed to me. However, since the mapped composite-id seems pretty useless as it works, I have thrown it out and now I use a normal composite-id without class (component) at all. It is uglyer, but it works.

So still I'm wondering what a mapped="true" composite-id is for, given the way it works it seems pretty useless to me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 9:03 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
read chapter 5.1.5 composite-id from documentation


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