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