Hibernate version: 3.0 rc 1
I used a properties element to define a unique index on two fields. I tried to build an HQL query using just the TransactionType property name. This always resulted in org.hibernate.QueryException: could not resolve property. (By the way, I used several versions of upper and lower case, and none of them worked.)
Code:
String queryString = "from jonstef.clcsc.Transaction as tx " +
"where tx.TransactionType.name = :type";
List results = getSession().createQuery(queryString)
.setString("type", transactionTypeName)
.list();
I found that I had to insert the name of the properties element in my HQL. This works:
Code:
String queryString = "from jonstef.clcsc.Transaction as tx " +
"where txUniqueProperties.TransactionType.name = :type";
List results = getSession().createQuery(queryString)
.setString("type", transactionTypeName)
.list();
Is this how we are meant to work with properties mappings in HQL? The UniqueProperties is nowhere to be found in my Java POJO, so it seems a little odd to have to use it in my HQL.
Mapping documents: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="jonstef.clcsc.Transaction" table="TRANSACTIONS">
<id name="id" type="int" column="TRANSACTION_ID">
<meta attribute="scope-set">protected</meta>
<generator class="identity"/>
</id>
<version
name="version"
type="integer">
<meta attribute="scope-set">protected</meta>
</version>
<many-to-one
name="MemberType"
class="jonstef.clcsc.MemberType"
not-null="true"
column="MEMBER_TYPE_ID"/>
<properties
name="UniqueProperties"
insert="true"
update="true"
unique="true">
<many-to-one
name="Member"
column="MEMBER_ID"
class="jonstef.clcsc.Member"
not-null="true"/>
<many-to-one
name="TransactionType"
class="jonstef.clcsc.TransactionType"
not-null="true"/>
</properties>
<set name="payments" lazy="true" cascade="save-update,lock">
<key column="TRANSACTION_ID"/>
<one-to-many class="jonstef.clcsc.Payment"/>
</set>
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
2005-03-08 18:56:40,962 [main] DEBUG org.hibernate.hql.AST - --- HQL AST ---
\-[QUERY] CommonAST: 'query'
+-[SELECT_FROM] CommonAST: 'SELECT_FROM'
| \-[FROM] CommonAST: 'from'
| +-[DOT] CommonAST: '.'
| | +-[DOT] CommonAST: '.'
| | | +-[IDENT] CommonAST: 'jonstef'
| | | \-[IDENT] CommonAST: 'clcsc'
| | \-[IDENT] CommonAST: 'Transaction'
| \-[ALIAS] CommonAST: 'tx'
\-[WHERE] CommonAST: 'where'
\-[EQ] CommonAST: '='
+-[DOT] CommonAST: '.'
| +-[DOT] CommonAST: '.'
| | +-[IDENT] CommonAST: 'tx'
| | \-[IDENT] CommonAST: 'TransactionType'
| \-[IDENT] CommonAST: 'name'
\-[COLON] CommonAST: ':'
\-[IDENT] CommonAST: 'type'
2005-03-08 18:56:41,002 [main] DEBUG org.hibernate.hql.antlr.HqlSqlBaseWalker - query() << begin, level = 1
2005-03-08 18:56:41,022 [main] DEBUG org.hibernate.hql.ast.FromElement - jonstef.clcsc.Transaction (tx) -> transactio0_
2005-03-08 18:56:41,022 [main] DEBUG org.hibernate.hql.ast.FromReferenceNode - Resolved : tx -> transactio0_.TRANSACTION_ID
2005-03-08 18:56:41,022 [main] ERROR jonstef.clcsc.data.DataLoader - Error executing query.
org.hibernate.QueryException: could not resolve property: TransactionType of: jonstef.clcsc.Transaction [from jonstef.clcsc.Transaction as tx where tx.TransactionType.name = :type]
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.toType(BasicEntityPersister.java:1108)
at org.hibernate.hql.ast.FromElementType.getPropertyType(FromElementType.java:223)
at org.hibernate.hql.ast.FromElement.getPropertyType(FromElement.java:333)
at org.hibernate.hql.ast.DotNode.getDataType(DotNode.java:439)
at org.hibernate.hql.ast.DotNode.prepareLhs(DotNode.java:205)
at org.hibernate.hql.ast.DotNode.resolve(DotNode.java:165)
at org.hibernate.hql.ast.DotNode.resolveFirstChild(DotNode.java:133)
at org.hibernate.hql.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:331)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:749)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:820)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:3209)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:2690)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1321)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:584)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:389)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:184)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:140)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:773)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at jonstef.clcsc.data.DataLoader.getMembershipTotals(DataLoader.java:551)
at jonstef.clcsc.data.DataLoader.testQuery(DataLoader.java:536)
at jonstef.clcsc.data.DataLoader.main(DataLoader.java:622)
Name and version of the database you are using: hsqldb