-->
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: Select based of ONE of the fields of a composite-id
PostPosted: Mon Jun 27, 2005 5:06 am 
Newbie

Joined: Wed Jun 15, 2005 8:51 am
Posts: 11
Hi,

I'm using Hibernate 3.0.5.
I would like to create a Criteria, Query or .... that results in a Collection.
The selection is based on one of the fields of the composite-id

How to make such a Criteria.

My Mapping file:
Code:
<?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="X" table="X" schema="Y">
        <composite-id name="id" class="XId">
            <key-property name="nr1" type="long">
                <column name="NR1" precision="10" scale="0" />
            </key-property>
            <key-property name="nr21" type="short">
                <column name="NR21" precision="3" scale="0" />
            </key-property>
        </composite-id>
        <property name="date" type="short">
            <column name="DATE" precision="3" scale="0" />
        </property>
    </class>
</hibernate-mapping>


And here's what I tried:
(I want all X's where nr1 has the given parameter).
Code:
criteria = session.createCriteria(X.class);
criteria.add(
                    Restrictions.eq("nr1", nr1)).addOrder(
                    Property.forName("date").desc());
this.addAll (criteria.list());


Then I get:
Quote:
org.hibernate.QueryException: could not resolve property: nr1 of: X
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.BasicEntityPersister.getSubclassPropertyTableNumber(BasicEntityPersister.java:1111)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.BasicEntityPersister.toColumns(BasicEntityPersister.java:1086)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:403)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:369)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:42)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at x.testNr1(PerformanceTestCase.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)


And another try:
Code:
Criteria criteria = session.createCriteria(X.class);
criteria.add(
                    Restrictions.eq("nr1", nr1));


No result! (I'm sure I should get a result).

(I rather have a real ID of only 1 field, but the table already exists and can't be changed)

Hope anybody knows how to to this right!

Thanx in advance,
Marcel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 7:53 am 
Newbie

Joined: Wed Jun 15, 2005 8:51 am
Posts: 11
I found a way to do this:
Code:
<property name="nr1" type="long" insert="false" update="false">
          <column name="NR1" precision="10" scale="0" />
</property>


I added an extra 'read only' property to the Mapping file.

But is this a correct way of doing these kind of things?

Marcel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 8:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Did you try something like Restrictions.eq("id.nr1", nr1)? At least in HQL this should work:

from X x where x.id.nr1 = ? order by date desc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 8:36 am 
Newbie

Joined: Wed Jun 15, 2005 8:51 am
Posts: 11
Oke, this is it!!

Code:
criteria.add(Restrictions.eq("id.nr1", nr1))


Thanks a lot


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.