Hibernate version: 3.0
Mapping documents:
Code:
<hibernate-mapping>
<class name="org.gbif.hibernate.tcn.Taxa" table="taxa">
<id name="recordId" column="record_id" type="long">
<generator class="native">
</generator>
</id>
<property name="name" type="java.lang.String" update="true" insert="true"
column="name"/>
<property name="nameCode" type="java.lang.String" update="true"
insert="true" column="name_code"/>
<property name="taxon" type="java.lang.String" update="true" insert="true"
column="taxon"/>
<set name="commonNames" lazy="false">
<key column="name_code" property-ref="nameCode"/>
<one-to-many class="org.gbif.hibernate.tcn.CommonName"/>
</set>
</class>
</hibernate-mapping>
Has a set of (note that the join is NOT on the primary key - it's on a name_code that is on both tables)
Code:
<hibernate-mapping>
<class name="org.gbif.hibernate.tcn.CommonName" table="common_names">
<id name="recordId" column="record_id" type="long">
<generator class="native">
</generator>
</id>
<property name="commonName" type="java.lang.String" update="true"
insert="true" column="common_name"/>
<property name="nameCode" type="java.lang.String" update="true"
insert="true" column="name_code"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
List<Taxa> results = (List<Taxa>) session.createQuery("from Taxa where name like('%a%') and taxon='Kingdom'").list();
Full stack trace of any exception that occurs:Code:
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:791)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:228)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:298)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1020)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at org.gbif.hibernate.tcn.Test.listTaxaByNameAndRank(Test.java:45)
at org.gbif.hibernate.tcn.Test.run(Test.java:27)
at org.gbif.hibernate.tcn.Test.main(Test.java:66)
Name and version of the database you are using:MySQL 4.1
The generated SQL (show_sql=true):Code:
Hibernate: select taxa0_.record_id as record1_0_, taxa0_.name as name0_, taxa0_.name_code as name3_0_, taxa0_.taxon as taxon0_ from taxa taxa0_ where (taxa0_.name like '%a%') and taxa0_.taxon='Kingdom'
Hibernate: select commonname0_.name_code as name3_1_, commonname0_.record_id as record1_1_, commonname0_.record_id as record1_1_0_, commonname0_.common_name as common2_1_0_, commonname0_.name_code as name3_1_0_ from common_names commonname0_ where commonname0_.name_code=?
Debug level Hibernate log excerpt:Code:
DEBUG [SessionImpl] opened session at timestamp: 4657754688446464
DEBUG [SessionImpl] find: from Taxa where name like('%a%') and taxon='Kingdom'
DEBUG [QueryParameters] named parameters: {}
DEBUG [QueryTranslatorImpl] parse() - HQL: from org.gbif.hibernate.tcn.Taxa where name like('%a%') and taxon='Kingdom'
DEBUG [AST] --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| \-[FROM] 'from'
| \-[RANGE] 'RANGE'
| \-[DOT] '.'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[IDENT] 'org'
| | | | \-[IDENT] 'gbif'
| | | \-[IDENT] 'hibernate'
| | \-[IDENT] 'tcn'
| \-[IDENT] 'Taxa'
\-[WHERE] 'where'
\-[AND] 'and'
+-[LIKE] 'like'
| +-[IDENT] 'name'
| \-[QUOTED_STRING] ''%a%''
\-[EQ] '='
+-[IDENT] 'taxon'
\-[QUOTED_STRING] ''Kingdom''
DEBUG [ErrorCounter] throwQueryException() : no errors
DEBUG [HqlSqlBaseWalker] select << begin [level=1, statement=select]
DEBUG [FromElement] FromClause{level=1} : org.gbif.hibernate.tcn.Taxa (no alias) -> taxa0_
DEBUG [FromReferenceNode] Resolved : name -> taxa0_.name
DEBUG [FromReferenceNode] Resolved : taxon -> taxa0_.taxon
DEBUG [HqlSqlBaseWalker] select : finishing up [level=1, statement=select]
DEBUG [HqlSqlWalker] processQuery() : ( SELECT ( FromClause{level=1} taxa taxa0_ ) ( where ( and ( like taxa0_.name '%a%' ) ( = taxa0_.taxon 'Kingdom' ) ) ) )
DEBUG [HqlSqlWalker] Derived SELECT clause created.
DEBUG [JoinProcessor] Using FROM fragment [taxa taxa0_]
DEBUG [HqlSqlBaseWalker] select >> end [level=1, statement=select]
DEBUG [AST] --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (taxa)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'taxa0_.record_id as record1_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=taxa,tableAlias=taxa0_,origin=null,colums={,className=org.gbif.hibernate.tcn.Taxa}}}
| \-[SQL_TOKEN] SqlFragment: 'taxa0_.name as name0_, taxa0_.name_code as name3_0_, taxa0_.taxon as taxon0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[taxa0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'taxa taxa0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=taxa,tableAlias=taxa0_,origin=null,colums={,className=org.gbif.hibernate.tcn.Taxa}}
\-[WHERE] SqlNode: 'where'
\-[AND] SqlNode: 'and'
+-[LIKE] BinaryLogicOperatorNode: 'like'
| +-[SQL_TOKEN] IdentNode: 'taxa0_.name' {originalText=name}
| \-[QUOTED_STRING] LiteralNode: ''%a%''
\-[EQ] BinaryLogicOperatorNode: '='
+-[SQL_TOKEN] IdentNode: 'taxa0_.taxon' {originalText=taxon}
\-[QUOTED_STRING] LiteralNode: ''Kingdom''
DEBUG [ErrorCounter] throwQueryException() : no errors
DEBUG [QueryTranslatorImpl] HQL: from org.gbif.hibernate.tcn.Taxa where name like('%a%') and taxon='Kingdom'
DEBUG [QueryTranslatorImpl] SQL: select taxa0_.record_id as record1_0_, taxa0_.name as name0_, taxa0_.name_code as name3_0_, taxa0_.taxon as taxon0_ from taxa taxa0_ where (taxa0_.name like '%a%') and taxa0_.taxon='Kingdom'
DEBUG [ErrorCounter] throwQueryException() : no errors
DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [ConnectionManager] opening JDBC connection
DEBUG [DriverManagerConnectionProvider] total checked-out connections: 0
DEBUG [DriverManagerConnectionProvider] using pooled JDBC connection, pool size: 0
DEBUG [SQL] select taxa0_.record_id as record1_0_, taxa0_.name as name0_, taxa0_.name_code as name3_0_, taxa0_.taxon as taxon0_ from taxa taxa0_ where (taxa0_.name like '%a%') and taxa0_.taxon='Kingdom'
Hibernate: select taxa0_.record_id as record1_0_, taxa0_.name as name0_, taxa0_.name_code as name3_0_, taxa0_.taxon as taxon0_ from taxa taxa0_ where (taxa0_.name like '%a%') and taxa0_.taxon='Kingdom'
DEBUG [AbstractBatcher] preparing statement
DEBUG [AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [Loader] processing result set
DEBUG [Loader] result set row: 0
DEBUG [LongType] returning '1241647' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [StringType] returning 'Animalia' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] result set row: 1
DEBUG [LongType] returning '1246353' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [StringType] returning 'Archaea' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] result set row: 2
DEBUG [LongType] returning '1246388' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [StringType] returning 'Bacteria' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] result set row: 3
DEBUG [LongType] returning '1246650' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [StringType] returning 'Chromista' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] result set row: 4
DEBUG [LongType] returning '1247696' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [StringType] returning 'Plantae' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] result set row: 5
DEBUG [LongType] returning '1248864' as column: record1_0_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [StringType] returning 'Protozoa' as column: name0_
DEBUG [StringType] returning '' as column: name3_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_
DEBUG [Loader] done processing result set (6 rows)
DEBUG [AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [AbstractBatcher] closing statement
DEBUG [Loader] total objects hydrated: 6
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [CollectionLoadContext] creating collection wrapper:[org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [StatefulPersistenceContext] initializing non-lazy collections
DEBUG [DefaultInitializeCollectionEventListener] initializing collection [org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [DefaultInitializeCollectionEventListener] checking second-level cache
DEBUG [DefaultInitializeCollectionEventListener] collection not cached
DEBUG [Loader] loading collection: [org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [SQL] select commonname0_.name_code as name3_1_, commonname0_.record_id as record1_1_, commonname0_.record_id as record1_1_0_, commonname0_.common_name as common2_1_0_, commonname0_.name_code as name3_1_0_ from common_names commonname0_ where commonname0_.name_code=?
Hibernate: select commonname0_.name_code as name3_1_, commonname0_.record_id as record1_1_, commonname0_.record_id as record1_1_0_, commonname0_.common_name as common2_1_0_, commonname0_.name_code as name3_1_0_ from common_names commonname0_ where commonname0_.name_code=?
DEBUG [AbstractBatcher] preparing statement
DEBUG [StringType] binding '' to parameter: 1
DEBUG [AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [Loader] result set contains (possibly empty) collection: [org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [CollectionLoadContext] uninitialized collection: initializing
DEBUG [Loader] processing result set
DEBUG [Loader] done processing result set (0 rows)
DEBUG [AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [AbstractBatcher] closing statement
DEBUG [Loader] total objects hydrated: 0
DEBUG [CollectionLoadContext] 1 collections were found in result set for role: org.gbif.hibernate.tcn.Taxa.commonNames
DEBUG [CollectionLoadContext] collection fully initialized: [org.gbif.hibernate.tcn.Taxa.commonNames#]
DEBUG [CollectionLoadContext] 1 collections initialized for role: org.gbif.hibernate.tcn.Taxa.commonNames
DEBUG [Loader] done loading collection
DEBUG [DefaultInitializeCollectionEventListener] collection initialized
DEBUG [JDBCContext] after autocommit
DEBUG [ConnectionManager] aggressively releasing JDBC connection
DEBUG [ConnectionManager] closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [DriverManagerConnectionProvider] returning connection to pool, pool size: 1
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:791)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:228)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:298)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1020)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at org.gbif.hibernate.tcn.Test.listTaxaByNameAndRank(Test.java:45)
at org.gbif.hibernate.tcn.Test.run(Test.java:27)
at org.gbif.hibernate.tcn.Test.main(Test.java:66)
Thanks for looking.