Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3
Mapping documents:
Code:
<hibernate-mapping>
<class name="org.gbif.hibernate.tcn.Taxa" table="taxa">
<id name="recordId" column="record_id" type="java.lang.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="true">
<key column="name_code" property-ref="nameCode"/>
<one-to-many class="org.gbif.hibernate.tcn.CommonName"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="org.gbif.hibernate.tcn.CommonName" table="common_names">
<id name="recordId" column="record_id" type="java.lang.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 taxa left join fetch taxa.commonNames where taxa.taxon='Kingdom'").list();
for (Taxa taxa : results) {
Set<CommonName> commonNames = (Set<CommonName>) taxa.getCommonNames(); // this returns null
for (CommonName commonName : commonNames) { // throws the exception
System.out.println(" " + commonName.getCommonName());
}
}
Full stack trace of any exception that occurs:Code:
java.lang.NullPointerException
at org.gbif.hibernate.tcn.Test.test(Test.java:45)
at org.gbif.hibernate.tcn.Test.run(Test.java:20)
at org.gbif.hibernate.tcn.Test.main(Test.java:93)
Name and version of the database you are using:Mysql 4.1
The generated SQL (show_sql=true):Code:
select taxa0_.record_id as record1_0_0_, commonname1_.record_id as record1_1_1_, taxa0_.name as name0_0_, taxa0_.name_code as name3_0_0_, taxa0_.taxon as taxon0_0_, commonname1_.common_name as common2_1_1_, commonname1_.name_code as name3_1_1_, commonname1_.name_code as name3_0__, commonname1_.record_id as record1_0__ from taxa taxa0_ left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code where taxa0_.taxon='Kingdom'
Debug level Hibernate log excerpt:Code:
DEBUG [SessionImpl] opened session at timestamp: 4657808856064000
DEBUG [SessionImpl] find: from Taxa taxa left join fetch taxa.commonNames where taxa.taxon='Kingdom'
DEBUG [QueryParameters] named parameters: {}
DEBUG [QueryTranslatorImpl] parse() - HQL: from org.gbif.hibernate.tcn.Taxa taxa left join fetch taxa.commonNames where taxa.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'
| | \-[ALIAS] 'taxa'
| \-[JOIN] 'join'
| +-[LEFT] 'left'
| +-[FETCH] 'fetch'
| \-[DOT] '.'
| +-[IDENT] 'taxa'
| \-[IDENT] 'commonNames'
\-[WHERE] 'where'
\-[EQ] '='
+-[DOT] '.'
| +-[IDENT] 'taxa'
| \-[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 (taxa) -> taxa0_
DEBUG [FromReferenceNode] Resolved : taxa -> taxa0_.record_id
DEBUG [DotNode] getDataType() : commonNames -> org.hibernate.type.SetType(org.gbif.hibernate.tcn.Taxa.commonNames)
DEBUG [FromElementFactory] createEntityAssociation() : One to many - path = taxa.commonNames role = org.gbif.hibernate.tcn.Taxa.commonNames associatedEntityName = org.gbif.hibernate.tcn.CommonName
DEBUG [FromElement] FromClause{level=1} : org.gbif.hibernate.tcn.CommonName (no alias) -> commonname1_
DEBUG [FromClause] addJoinByPathMap() : taxa.commonNames -> common_names commonname1_
DEBUG [DotNode] dereferenceCollection() : Created new FROM element for taxa.commonNames : common_names commonname1_
DEBUG [FromReferenceNode] Resolved : taxa.commonNames -> .
DEBUG [HqlSqlWalker] createFromJoinElement() : -- join tree --
\-[JOIN_FRAGMENT] FromElement: 'common_names commonname1_' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=org.gbif.hibernate.tcn.Taxa.commonNames,tableName=common_names,tableAlias=commonname1_,origin=taxa taxa0_,colums={taxa0_.name_code ,className=org.gbif.hibernate.tcn.CommonName}}
DEBUG [FromReferenceNode] Resolved : taxa -> taxa0_.record_id
DEBUG [DotNode] getDataType() : taxon -> org.hibernate.type.StringType@1f297e7
DEBUG [FromReferenceNode] Resolved : taxa.taxon -> taxa0_.taxon
DEBUG [HqlSqlBaseWalker] select : finishing up [level=1, statement=select]
DEBUG [HqlSqlWalker] processQuery() : ( SELECT ( FromClause{level=1} ( taxa taxa0_ common_names commonname1_ ) ) ( where ( = ( taxa0_.taxon taxa0_.record_id taxon ) 'Kingdom' ) ) )
DEBUG [HqlSqlWalker] Derived SELECT clause created.
DEBUG [JoinProcessor] Using FROM fragment [left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code]
DEBUG [JoinProcessor] Using FROM fragment [taxa taxa0_]
DEBUG [HqlSqlBaseWalker] select >> end [level=1, statement=select]
DEBUG [AST] --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (taxa,common_names)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'taxa0_.record_id as record1_0_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=taxa,role=null,tableName=taxa,tableAlias=taxa0_,origin=null,colums={,className=org.gbif.hibernate.tcn.Taxa}}}
| +-[SELECT_EXPR] SelectExpressionImpl: 'commonname1_.record_id as record1_1_1_' {FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=org.gbif.hibernate.tcn.Taxa.commonNames,tableName=common_names,tableAlias=commonname1_,origin=taxa taxa0_,colums={taxa0_.name_code ,className=org.gbif.hibernate.tcn.CommonName}}}
| +-[SQL_TOKEN] SqlFragment: 'taxa0_.name as name0_0_, taxa0_.name_code as name3_0_0_, taxa0_.taxon as taxon0_0_'
| +-[SQL_TOKEN] SqlFragment: 'commonname1_.common_name as common2_1_1_, commonname1_.name_code as name3_1_1_'
| \-[SQL_TOKEN] SqlFragment: 'commonname1_.name_code as name3_0__, commonname1_.record_id as record1_0__'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=2, fromElements=2, fromElementByClassAlias=[taxa], fromElementByTableAlias=[commonname1_, taxa0_], fromElementsByPath=[taxa.commonNames], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'taxa taxa0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=taxa,role=null,tableName=taxa,tableAlias=taxa0_,origin=null,colums={,className=org.gbif.hibernate.tcn.Taxa}}
| \-[JOIN_FRAGMENT] FromElement: 'left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=org.gbif.hibernate.tcn.Taxa.commonNames,tableName=common_names,tableAlias=commonname1_,origin=taxa taxa0_,colums={taxa0_.name_code ,className=org.gbif.hibernate.tcn.CommonName}}
\-[WHERE] SqlNode: 'where'
\-[EQ] BinaryLogicOperatorNode: '='
+-[DOT] DotNode: 'taxa0_.taxon' {propertyName=taxon,dereferenceType=4,propertyPath=taxon,path=taxa.taxon,tableAlias=taxa0_,className=org.gbif.hibernate.tcn.Taxa,classAlias=taxa}
| +-[ALIAS_REF] IdentNode: 'taxa0_.record_id' {alias=taxa, className=org.gbif.hibernate.tcn.Taxa, tableAlias=taxa0_}
| \-[IDENT] IdentNode: 'taxon' {originalText=taxon}
\-[QUOTED_STRING] LiteralNode: ''Kingdom''
DEBUG [ErrorCounter] throwQueryException() : no errors
DEBUG [QueryTranslatorImpl] HQL: from org.gbif.hibernate.tcn.Taxa taxa left join fetch taxa.commonNames where taxa.taxon='Kingdom'
DEBUG [QueryTranslatorImpl] SQL: select taxa0_.record_id as record1_0_0_, commonname1_.record_id as record1_1_1_, taxa0_.name as name0_0_, taxa0_.name_code as name3_0_0_, taxa0_.taxon as taxon0_0_, commonname1_.common_name as common2_1_1_, commonname1_.name_code as name3_1_1_, commonname1_.name_code as name3_0__, commonname1_.record_id as record1_0__ from taxa taxa0_ left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code where 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_0_, commonname1_.record_id as record1_1_1_, taxa0_.name as name0_0_, taxa0_.name_code as name3_0_0_, taxa0_.taxon as taxon0_0_, commonname1_.common_name as common2_1_1_, commonname1_.name_code as name3_1_1_, commonname1_.name_code as name3_0__, commonname1_.record_id as record1_0__ from taxa taxa0_ left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code where taxa0_.taxon='Kingdom'
Hibernate: select taxa0_.record_id as record1_0_0_, commonname1_.record_id as record1_1_1_, taxa0_.name as name0_0_, taxa0_.name_code as name3_0_0_, taxa0_.taxon as taxon0_0_, commonname1_.common_name as common2_1_1_, commonname1_.name_code as name3_1_1_, commonname1_.name_code as name3_0__, commonname1_.record_id as record1_0__ from taxa taxa0_ left outer join common_names commonname1_ on taxa0_.name_code=commonname1_.name_code where 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_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1241647], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 1
DEBUG [LongType] returning '1246353' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246353], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 2
DEBUG [LongType] returning '1246388' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246388], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 3
DEBUG [LongType] returning '1246650' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1246650], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 4
DEBUG [LongType] returning '1247025' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1247025], null
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1247025]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1247025]
DEBUG [StringType] returning 'Fungi' as column: name0_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 5
DEBUG [LongType] returning '1247696' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1247696], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 6
DEBUG [LongType] returning '1248864' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1248864], null
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_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] result set row: 7
DEBUG [LongType] returning '1249283' as column: record1_0_0_
DEBUG [LongType] returning null as column: record1_1_1_
DEBUG [Loader] result row: EntityKey[org.gbif.hibernate.tcn.Taxa#1249283], null
DEBUG [Loader] Initializing object from ResultSet: [org.gbif.hibernate.tcn.Taxa#1249283]
DEBUG [AbstractEntityPersister] Hydrating entity: [org.gbif.hibernate.tcn.Taxa#1249283]
DEBUG [StringType] returning 'Viruses' as column: name0_0_
DEBUG [StringType] returning null as column: name3_0_0_
DEBUG [StringType] returning 'Kingdom' as column: taxon0_0_
DEBUG [StringType] returning null as column: name3_0__
DEBUG [Loader] done processing result set (8 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: 8
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1241647]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246353]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246388]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1246650]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1247025]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1247025]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1247696]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1248864]
DEBUG [TwoPhaseLoad] resolving associations for [org.gbif.hibernate.tcn.Taxa#1249283]
DEBUG [TwoPhaseLoad] done materializing entity [org.gbif.hibernate.tcn.Taxa#1249283]
DEBUG [CollectionLoadContext] 0 collections were found in result set for role: org.gbif.hibernate.tcn.Taxa.commonNames
DEBUG [CollectionLoadContext] 0 collections initialized for role: org.gbif.hibernate.tcn.Taxa.commonNames
DEBUG [StatefulPersistenceContext] initializing non-lazy collections
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
java.lang.NullPointerException
at org.gbif.hibernate.tcn.Test.test(Test.java:45)
at org.gbif.hibernate.tcn.Test.run(Test.java:20)
at org.gbif.hibernate.tcn.Test.main(Test.java:93)