-->
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.  [ 9 posts ] 
Author Message
 Post subject: id property in a HQL query involving component not found.
PostPosted: Sun Apr 11, 2004 9:01 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
I'm using Hibernate 2.1.2 in a Spring managed configuration. I have an entity "Party" which has an relationship to itself, managed with a component because there are attributes on the relationship (notably the relationship type, another entity). I have a problem with an HQL query, which seems to be unable to retrieve the "id" property on an entity contained in a component.

The error I recieve is:
could not resolve property: id of: it.esselunga.ecommerce.data.model.PartyRelationship [select partyRelationship from it.esselunga.ecommerce.data.model.Party party join party.relatedPartiesFrom partyRelationship where party.partyId = ? and partyRelationship.relatedParty.id = ? and partyRelationship.relationshipType.id = ? ];

and what seems strange is the fact that it refers to "id" of "PartyRelationship" (which is the component class), but I'm trying to refer to the "id" component of the contained entities!

Simplified mapping files are:

<hibernate-mapping>
<class name="it.esselunga.ecommerce.data.model.Party" table="PARTY" lazy="true" >
<id name="partyId" type="int" column="PARTY_ID">
<generator class="sequence">
<param name="sequence">PARTY_SEQ</param>
</generator>
</id>

<set name="relatedPartiesFrom" lazy="true" table="PARTY_RELATIONSHIP">
<key column="PARTY_ID_TO"/>
<composite-element class="it.esselunga.ecommerce.data.model.PartyRelationship">
<many-to-one name="relationshipType" class="it.esselunga.ecommerce.data.model.PartyRelationshipType">
<column name="RELATIONSHIP_TYPE_ID"/>
</many-to-one>
<many-to-one name="relatedParty" class="it.esselunga.ecommerce.data.model.Party" column="PARTY_ID_FROM"/>
</composite-element>
</set>

<set name="relatedPartiesTo" lazy="true" table="PARTY_RELATIONSHIP">
<key column="PARTY_ID_FROM"/>
<composite-element class="it.esselunga.ecommerce.data.model.PartyRelationship">
<many-to-one name="relationshipType" class="it.esselunga.ecommerce.data.model.PartyRelationshipType">
<column name="RELATIONSHIP_TYPE_ID"/>
</many-to-one>
<many-to-one name="relatedParty" class="it.esselunga.ecommerce.data.model.Party" column="PARTY_ID_TO"/>
</composite-element>
</set>
</hibernate-mapping>

The relationship type is a value of a subclass of a persistent entity called Enumeration:

<hibernate-mapping>
<class name="it.esselunga.ecommerce.data.model.Enumeration" table="ENUMERATION">
<id name="enumId" type="int" column="ENUM_ID" length="10" >
<generator class="sequence">
<param name="sequence">ENUM_SEQ</param>
</generator>
</id>
<discriminator length="32" column="ENUM_TYPE"/>
<property name="enumCode" type="java.lang.String" not-null="true" column="ENUM_CODE" length="32">
</property>
<property name="sequence" type="int" column="SEQUENCE" length="3">
</property>
<property name="description" type="java.lang.String" column="DESCRIPTION" length="100">
</property>
<subclass name="it.esselunga.ecommerce.data.model.Profession" discriminator-value="Profession" />
<subclass name="it.esselunga.ecommerce.data.model.EducationLevel" discriminator-value="EducationLevel"/>
<subclass name="it.esselunga.ecommerce.data.model.PartyRelationshipType" discriminator-value="PartyRelationshipType">
<subclass name="it.esselunga.ecommerce.data.model.RelativeType" discriminator-value="RelativeType"/>
</subclass>
</class>
</hibernate-mapping>

What am I doing wrong?

Thanks anyone,
Davide Baroncelli.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 10:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please submit code to reproduce this (as a very simple easy to run main() method) to JIRA.

What happens if you use this query instead:

Code:
select partyRelationship from Party party
  join party.relatedPartiesFrom partyRelationship
where party.partyId = ?
  and partyRelationship.relatedParty = ?
  and partyRelationship.relationshipType = ?



Actually, what happens with just this query:

Code:
select partyRelationship from Party party
  join party.relatedPartiesFrom partyRelationship


Lastly, are you calling the query with find(), or iterate()?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 10:49 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
gavin wrote:
Please submit code to reproduce this (as a very simple easy to run main() method) to JIRA.


Ok.


gavin wrote:
What happens if you use this query instead:

Code:
select partyRelationship from Party party
  join party.relatedPartiesFrom partyRelationship
where party.partyId = ?
  and partyRelationship.relatedParty = ?
  and partyRelationship.relationshipType = ?



Same thing (?!):
could not resolve property: id of: it.esselunga.ecommerce.data.model.PartyRelationship [select partyRelationship from it.esselunga.ecommerce.data.model.Party party join party.relatedPartiesFrom partyRelationship where party.partyId = ? and partyRelationship.relatedParty = ? and partyRelationship.relationshipType = ? ]

gavin wrote:
Actually, what happens with just this query:

Code:
select partyRelationship from Party party
  join party.relatedPartiesFrom partyRelationship



Same thing again (!!!):
could not resolve property: id of: it.esselunga.ecommerce.data.model.PartyRelationship [select partyRelationship from it.esselunga.ecommerce.data.model.Party party join party.relatedPartiesFrom partyRelationship ]

gavin wrote:
Lastly, are you calling the query with find(), or iterate()?


I a Query.uniqueResult.

The exact code is:
1) calling the method from a junit test
Code:
assertTrue( partyService.isRelatedTo( partyContainer.customer, partyContainer.person[ 0 ],
                                              partyContainer.partyRelationshipType[ 0 ] ) );


2) the method is implemented by a Spring bean with transactions and session managed by TransactionInterceptor
Code:
    public boolean isRelatedTo( Person partyRelatedFrom, Person partyRelatedTo, PartyRelationshipType relType ) {
        PartyRelationship retrieved =
                ( PartyRelationship )DataAccessUtils.uniqueResult( "select partyRelationship " +
                                                                   "from Party party " +
                                                                   "join party.relatedPartiesFrom partyRelationship " +
                                                                   "where party.partyId = ? " +
                                                                   "and partyRelationship.relatedParty = ? " +
                                                                   "and partyRelationship.relationshipType = ? ",
                                                                   new Object[] { partyRelatedFrom, partyRelatedTo,
                                                                                  relType },
                                                                   new Type[] { Hibernate.OBJECT, Hibernate.OBJECT,
                                                                                Hibernate.OBJECT},
                                                                   sessionFactory );
        return retrieved != null;
    }


Finally, the DataAccessUtils.uniqueResult is a helper method whose code is:

Code:
    public static Object uniqueResult( String queryString, Object[] paramValues, Type[] paramTypes,
                                       SessionFactory sessionFactory ) {
        Session session = SessionFactoryUtils.getSession( sessionFactory, false );
        try {
            // @todo check this is loaded from a cache
            Query query = session.createQuery( queryString );
            for ( int i = 0 ; i < paramValues.length ; i++ ) {
                Object paramValue = paramValues[ i ];
                Type paramType = paramTypes[ i ];
                query.setParameter( i, paramValue, paramType );
            }
            Object object = query.uniqueResult();
            return object;
        }
        catch ( HibernateException e ) {
            throw SessionFactoryUtils.convertHibernateAccessException( e );
        }
    }


Thanks, now I'll try to reproduce with a simple example the problem and file it to JIRA.

N.B: I have another problem which may be related to this but maybe has nothing to do and it's only my fault (I'm afraid the mapping with the double from - to and to-from relationship may be responsible of this). Since I'm trying to implement a "isRelatedTo" method which returns true if two parties are lied by a relationship of a given type, I am looking for a workaround. So I tried with the following code:

Code:
        Session session = SessionFactoryUtils.getSession( sessionFactory, false );
        try {
            Party partyRelatedFrom = ( Party )session.load( Party.class, partyIdRelatedFrom );
            Set relatedPartiesFrom = partyRelatedFrom.getRelatedPartiesFrom();
            for ( Iterator iterator = relatedPartiesFrom.iterator() ; iterator.hasNext() ; ) {
                PartyRelationship partyRelationship = ( PartyRelationship )iterator.next();
                Party relatedParty = partyRelationship.getRelatedParty();
                if ( relatedParty != null && relatedParty.getPartyId().equals( partyIdRelatedTo ) ) {
                    if ( remove ) iterator.remove();
                    return true;
                }
            }
            return false;
        }
        catch ( HibernateException e ) {
            throw SessionFactoryUtils.convertHibernateAccessException( e );
        }


And this gives the following trace:
16:47:28,425 DEBUG TransactionInterceptor:146 - Getting transaction for method 'isRelatedTo' in class [it.esselunga.ecommerce.services.party.PartyService]
16:47:28,446 DEBUG HibernateTransactionManager:163 - Using transaction object [org.springframework.orm.hibernate.HibernateTransactionObject@1cb048e]
16:47:28,456 DEBUG HibernateTransactionManager:214 - Creating new transaction
16:47:28,466 DEBUG SessionFactoryUtils:156 - Opening Hibernate session
16:47:28,476 DEBUG HibernateTransactionManager:271 - Opened new session [net.sf.hibernate.impl.SessionImpl@1983ad7] for Hibernate transaction
16:47:28,496 DEBUG HibernateTransactionManager:280 - Beginning Hibernate transaction on session [net.sf.hibernate.impl.SessionImpl@1983ad7]
16:47:28,516 DEBUG TransactionSynchronizationManager:129 - Bound value [org.springframework.orm.hibernate.SessionHolder@13f348b] for key [net.sf.hibernate.impl.SessionFactoryImpl@c7833c] to thread [main]
16:47:28,536 DEBUG TransactionSynchronizationManager:129 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@92997e] for key [org.apache.commons.dbcp.BasicDataSource@ac4d3b] to thread [main]
16:47:28,546 DEBUG TransactionSynchronizationManager:175 - Initializing transaction synchronization

16:47:28,576 DEBUG TransactionSynchronizationManager:111 - Retrieved value [org.springframework.orm.hibernate.SessionHolder@13f348b] for key [net.sf.hibernate.impl.SessionFactoryImpl@c7833c] bound to thread [main]
16:47:28,596 DEBUG HibernateInterceptor:89 - Found thread-bound session for Hibernate interceptor
16:47:34,034 DEBUG SpringHibernatePartyService:52 - trying to retrieve relationship of type 94 between parties 50(from) and 49(to)
16:47:34,044 DEBUG TransactionSynchronizationManager:111 - Retrieved value [org.springframework.orm.hibernate.SessionHolder@13f348b] for key [net.sf.hibernate.impl.SessionFactoryImpl@c7833c] bound to thread [main]
Hibernate: select party0_.PARTY_ID as PARTY_ID2_, case when party0__2_.PARTY_ID is not null then 1 when party0__1_.PARTY_ID is not null then 2 when party0__3_.PARTY_ID is not null then 3 when party0_.PARTY_ID is not null then 0 end as clazz_2_, party0_.PARTY_TYPE as PARTY_TYPE8_2_, party0__1_.TITLE as TITLE10_2_, party0__1_.SURNAME as SURNAME10_2_, party0__1_.FORENAME as FORENAME10_2_, party0__1_.EMAIL as EMAIL10_2_, party0__1_.HOME_TEL as HOME_TEL10_2_, party0__1_.WORK_TEL as WORK_TEL10_2_, party0__1_.MOBILE_TEL as MOBILE_TEL10_2_, party0__1_.DATE_OF_BIRTH as DATE_OF_9_10_2_, party0__1_.SEX as SEX10_2_, party0__1_.DETAILS_PRIVATE as DETAILS11_10_2_, party0__1_.CREATION_DATE as CREATIO12_10_2_, party0__1_.PROFESSION_ID as PROFESS13_10_2_, party0__1_.EDUCATION_LEVEL_ID as EDUCATI14_10_2_, party0__2_.FIDELITY_CARD_TYPE as FIDELITY2_11_2_, party0__2_.FIDELITY_CARD_NUMBER as FIDELITY3_11_2_, party0__2_.IS_NEW_CARD_NUMBER as IS_NEW_C4_11_2_, party0__2_.HOW_MANY_RELATIVES as HOW_MANY5_11_2_, party0__2_.SEND_INFO as SEND_INFO11_2_, party0__3_.GROUP_NAME as GROUP_NAME12_2_, party0__3_.DESCRIPTION as DESCRIPT3_12_2_, profession1_.ENUM_ID as ENUM_ID0_, profession1_.ENUM_CODE as ENUM_CODE0_, profession1_.SEQUENCE as SEQUENCE0_, profession1_.DESCRIPTION as DESCRIPT5_0_, educationl2_.ENUM_ID as ENUM_ID1_, educationl2_.ENUM_CODE as ENUM_CODE1_, educationl2_.SEQUENCE as SEQUENCE1_, educationl2_.DESCRIPTION as DESCRIPT5_1_ from PARTY party0_ left outer join PERSON party0__1_ on party0_.PARTY_ID=party0__1_.PARTY_ID left outer join CUSTOMER party0__2_ on party0_.PARTY_ID=party0__2_.PARTY_ID left outer join PARTY_GROUP party0__3_ on party0_.PARTY_ID=party0__3_.PARTY_ID left outer join ENUMERATION profession1_ on party0__1_.PROFESSION_ID=profession1_.ENUM_ID left outer join ENUMERATION educationl2_ on party0__1_.EDUCATION_LEVEL_ID=educationl2_.ENUM_ID where party0_.PARTY_ID=?
Hibernate: select relatedpar0_.FROM_DATE as FROM_DATE__, relatedpar0_.THRU_DATE as THRU_DATE__, relatedpar0_.DESCRIPTION as DESCRIPT4___, relatedpar0_.RELATIONSHIP_TYPE_ID as RELATION5___, relatedpar0_.PARTY_ID_FROM as PARTY_ID6___, relatedpar0_.PARTY_ID_TO as PARTY_ID1___, partyrelat1_.ENUM_ID as ENUM_ID0_, partyrelat1_.ENUM_TYPE as ENUM_TYPE0_, partyrelat1_.ENUM_CODE as ENUM_CODE0_, partyrelat1_.SEQUENCE as SEQUENCE0_, partyrelat1_.DESCRIPTION as DESCRIPT5_0_ from PARTY_RELATIONSHIP relatedpar0_ left outer join ENUMERATION partyrelat1_ on relatedpar0_.RELATIONSHIP_TYPE_ID=partyrelat1_.ENUM_ID where relatedpar0_.PARTY_ID_TO=?
16:47:34,354 DEBUG HibernateInterceptor:109 - Not closing pre-bound Hibernate session after interceptor
16:47:34,364 DEBUG TransactionInterceptor:186 - Invoking commit for transaction on method 'isRelatedTo' in class [it.esselunga.ecommerce.services.party.PartyService]
16:47:34,374 DEBUG HibernateTransactionManager:415 - Triggering beforeCommit synchronization
16:47:34,374 DEBUG HibernateTransactionManager:430 - Triggering beforeCompletion synchronization
16:47:34,384 INFO HibernateTransactionManager:313 - Initiating transaction commit
16:47:34,384 DEBUG HibernateTransactionManager:382 - Committing Hibernate transaction on session [net.sf.hibernate.impl.SessionImpl@1983ad7]
Hibernate: delete from PARTY_RELATIONSHIP where PARTY_ID_TO=? and FROM_DATE=? and THRU_DATE=? and DESCRIPTION=? and RELATIONSHIP_TYPE_ID=? and PARTY_ID_FROM=?
Hibernate: insert into PARTY_RELATIONSHIP (PARTY_ID_TO, FROM_DATE, THRU_DATE, DESCRIPTION, RELATIONSHIP_TYPE_ID, PARTY_ID_FROM) values (?, ?, ?, ?, ?, ?)
16:47:34,614 WARN JDBCExceptionReporter:38 - SQL Error: 335544349, SQLState: HY000
16:47:34,624 ERROR JDBCExceptionReporter:46 - GDS Exception. 335544349. attempt to store duplicate value (visible to active transactions) in unique index "PK_PARTY_RELATIONSHIP"
16:47:34,634 ERROR JDBCExceptionReporter:38 - could not insert collection rows: [it.esselunga.ecommerce.data.model.Party.relatedPartiesFrom#50]
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544349. attempt to store duplicate value (visible to active transactions) in unique index "PK_PARTY_RELATIONSHIP"
at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:445)
at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:147)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:622)
at net.sf.hibernate.impl.ScheduledCollectionUpdate.execute(ScheduledCollectionUpdate.java:49)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2338)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:314)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:189)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:134)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
at $Proxy1.isRelatedTo(Unknown Source)
at it.esselunga.ecommerce.services.party.TestSpringHibernatePartyService.testPersonWithRelativeScenario(TestSpringHibernatePartyService.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
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.textui.TestRunner.doRun(TestRunner.java:116)
at com.intellij.rt.execution.junit2.IdeaJUnitAgent.doRun(Unknown Source)
at junit.textui.TestRunner.start(TestRunner.java:172)
at com.intellij.rt.execution.junit.TextTestRunner2.startRunnerWithArgs(Unknown Source)
at com.intellij.rt.execution.junit2.JUnitStarter.prepareStreamsAndStart(Unknown Source)
at com.intellij.rt.execution.junit2.JUnitStarter.main(Unknown Source)

After producing the jira test I'll try to look inside this one.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 10:52 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
n.b.: the "iterator.remove" is not called in the "isRelatedTo" method, it's simpy because I refactored the code from the cancellation method.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 11:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ugh. Sorry, just remembered a limitation of composite element queries: you must select a particular property, you can't return the "whole" component object. So this exception was expected. Duh. Forgot about that. (I do hope to fix this limitation at some stage.)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
P.S. You should make one of your collection mappings inverse="true"


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 11:52 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
gavin wrote:
Ugh. Sorry, just remembered a limitation of composite element queries: you must select a particular property, you can't return the "whole" component object. So this exception was expected. Duh. Forgot about that. (I do hope to fix this limitation at some stage.)


Ok, no problem, in my particular case this gives me a useful workaround. Thanks! I assume the test showing the problem occurrence is not needed anymore? I have it ready, anyway, if it is needed just tell me and I'll post it.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 11:54 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
gavin wrote:
P.S. You should make one of your collection mappings inverse="true"


Oh, thank you, I knew it and I was persuaded it was in there, but I was wrong! ;)

Thanks for your help, anyway.
Davide Baroncelli.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 12:15 pm 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
gavin wrote:
P.S. You should make one of your collection mappings inverse="true"


Everything works perfectly, now, and apart from the select limitation, all this stuff is very powerful (I now have generic typed relationships among parties, and they apply to every party subclass, this is perfect! :) ).

For what concerns the said limitation, may I suggest to:

1) explicitly telling about it in the user guide if it is not (although I've read the guide a couple of times and am translating it to italian, I'm not sure: it's not easy remembering all the stuff, it's pretty dense! ;) )

2) if it is possible, catch the error while parsing the HQL query and try to provide a more informative message (even if this thread could be a reference for similar problems in the future).

Thanks, Gavin, anyway!

P.s.: if there's some part in the wiki I could add the information resulting from this thread, please tell it to me and I will modify it.


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