We have a composite reference to a table, but one of the reference columns can be null. We don't have a problem when both fields are null, it also works OK when both are not null. The problem occurs when only one of them is null. It suppose is trying to do an immediate fetch of association.
I am able postpone the exception configuring Tb.hbm.xml with lazy="true"
but it also throws an exception when we access the referenced object. We don't have a way of knowing if the referenced object is null.
I have tried to set out-join=true in the <many-to-one> but it tries to eagerly fetch the object, and then it tries to do an immediate fetch which ends up throwing the same exception.
Is there a way to workaround this? I appreciate any help I can get.
Hibernate version:
2.1.6
Mapping documents:
Ta.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="example.Ta"
table="TA"
>
<id
name="idA"
type="java.lang.String"
column="ID_A"
>
<generator class="assigned" />
</id>
<!-- Associations -->
<!-- bi-directional many-to-one association to Tb -->
<many-to-one
name="tb"
class="example.Tb"
not-null="false"
>
<column name="ID_B1" />
<column name="ID_B2" />
</many-to-one>
</class>
</hibernate-mapping>
Tb.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="example.Tb"
table="TB"
>
<composite-id name="comp_id" class="example.TbPK">
<key-property
name="idB1"
column="ID_B1"
type="java.lang.String"
length="10"
/>
<key-property
name="idB2"
column="ID_B2"
type="java.lang.String"
length="10"
/>
</composite-id>
<property
name="descr"
type="java.lang.String"
column="DESCR"
length="20"
/>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->
<!-- bi-directional one-to-many association to Ta -->
<set
name="tas"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="ID_B1" />
<column name="ID_B2" />
</key>
<one-to-many
class="example.Ta"
/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = sf.openSession();
log.debug("loading AR1");
Ta ta = (Ta)session.load(Ta.class, "AR1");
log.debug("loaded AR1");
log.debug("loading AR2");
ta = (Ta)session.load(Ta.class, "AR2");
log.debug("loaded AR2");
session.close();
dbunit dataset used:
<TB
ID_B1="BR1"
ID_B2="BR1"
DESCR="registro 1" />
<TA
ID_A="AR1"
ID_B1="BR1"
ID_B2="BR1" />
<TA
ID_A="AR2"
ID_B1="BR1" />
Full stack trace of any exception that occurs:
Testcase: testExample(example.ExampleTest): Caused an ERROR
No row with the given identifier exists: null:null, of class: example.Tb
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: null:null, of class: example.Tb
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1954)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:204)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2205)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1920)
at example.ExampleTest.testExample(ExampleTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Name and version of the database you are using:
Oracle 10g
The generated SQL (show_sql=true):
------------- Standard Output ---------------
Hibernate: select ta0_.ID_A as ID_A1_, ta0_.ID_B1 as ID_B11_, ta0_.ID_B2 as ID_B21_, tb1_.ID_B1 as ID_B10_, tb1_.ID_B2 as ID_B20_, tb1_.DESCR as DESCR0_ from TA ta0_, TB tb1_ where ta0_.ID_A=? and ta0_.ID_B1=tb1_.ID_B1(+) and ta0_.ID_B2=tb1_.ID_B2(+)
Hibernate: select ta0_.ID_A as ID_A1_, ta0_.ID_B1 as ID_B11_, ta0_.ID_B2 as ID_B21_, tb1_.ID_B1 as ID_B10_, tb1_.ID_B2 as ID_B20_, tb1_.DESCR as DESCR0_ from TA ta0_, TB tb1_ where ta0_.ID_A=? and ta0_.ID_B1=tb1_.ID_B1(+) and ta0_.ID_B2=tb1_.ID_B2(+)
Hibernate: select tb0_.ID_B1 as ID_B10_, tb0_.ID_B2 as ID_B20_, tb0_.DESCR as DESCR0_ from TB tb0_ where tb0_.ID_B1=? and tb0_.ID_B2=?
------------- ---------------- ---------------
Debug level Hibernate log excerpt:
2005-08-14 22:35:24,906 [main] DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2005-08-14 22:35:24,906 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2005-08-14 22:35:24,906 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2005-08-14 22:35:24,906 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - conexionJDBC='oracle.jdbc.driver.OracleConnection@1bab2c3'
2005-08-14 22:35:24,906 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - catalog=[null]
2005-08-14 22:35:24,906 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - isClosed()=false
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - user=[HUHT]
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - esquema=[HUHT]
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - isClosed()=false
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - Termina inicializaConexion()
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - Llamando DatabaseOperation.CLEAN_INSERT.execute()
2005-08-14 22:35:24,921 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - Inicia getDataSet(src/test/datasets/example.xml)
2005-08-14 22:35:24,968 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - dataSet=[TB, TA]
2005-08-14 22:35:24,968 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - Termina getDataSet()
2005-08-14 22:35:25,140 [main] DEBUG mx.gob.imss.cia.spes.FachadaDBUnit - termina cleanInsert()
2005-08-14 22:35:25,140 [main] DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2005-08-14 22:35:25,140 [main] DEBUG example.ExampleTest - loading AR1
2005-08-14 22:35:25,140 [main] DEBUG net.sf.hibernate.impl.SessionImpl - loading [example.Ta#AR1]
2005-08-14 22:35:25,140 [main] DEBUG net.sf.hibernate.impl.SessionImpl - attempting to resolve [example.Ta#AR1]
2005-08-14 22:35:25,156 [main] DEBUG net.sf.hibernate.impl.SessionImpl - object not resolved in any cache [example.Ta#AR1]
2005-08-14 22:35:25,156 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Materializing entity: [example.Ta#AR1]
2005-08-14 22:35:25,156 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,156 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 1
2005-08-14 22:35:25,156 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - opening new JDBC connection
2005-08-14 22:35:25,312 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - created connection to: jdbc:oracle:thin:@192.168.20.243:1521:SPES, Isolation Level: 2
2005-08-14 22:35:25,312 [main] DEBUG net.sf.hibernate.SQL - select ta0_.ID_A as ID_A1_, ta0_.ID_B1 as ID_B11_, ta0_.ID_B2 as ID_B21_, tb1_.ID_B1 as ID_B10_, tb1_.ID_B2 as ID_B20_, tb1_.DESCR as DESCR0_ from TA ta0_, TB tb1_ where ta0_.ID_A=? and ta0_.ID_B1=tb1_.ID_B1(+) and ta0_.ID_B2=tb1_.ID_B2(+)
2005-08-14 22:35:25,312 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2005-08-14 22:35:25,312 [main] DEBUG net.sf.hibernate.type.StringType - binding 'AR1' to parameter: 1
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - processing result set
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - returning 'BR1' as column: ID_B10_
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - returning 'BR1' as column: ID_B20_
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - result row: BR1:null, AR1
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - Initializing object from ResultSet: BR1:null
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - Hydrating entity: example.Tb#BR1:null
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - returning 'registro 1' as column: DESCR0_
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - Initializing object from ResultSet: AR1
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - Hydrating entity: example.Ta#AR1
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - returning 'BR1' as column: ID_B11_
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - returning 'BR1' as column: ID_B21_
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - done processing result set (1 rows)
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.loader.Loader - total objects hydrated: 2
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - resolving associations for [example.Tb#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - creating collection wrapper:[example.Tb.tas#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - done materializing entity [example.Tb#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - resolving associations for [example.Ta#AR1]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - loading [example.Tb#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - attempting to resolve [example.Tb#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - resolved object in session cache [example.Tb#BR1:null]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - done materializing entity [example.Ta#AR1]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - initializing non-lazy collections
2005-08-14 22:35:25,328 [main] DEBUG example.ExampleTest - loaded AR1
2005-08-14 22:35:25,328 [main] DEBUG example.ExampleTest - loading AR2
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - loading [example.Ta#AR2]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - attempting to resolve [example.Ta#AR2]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.SessionImpl - object not resolved in any cache [example.Ta#AR2]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Materializing entity: [example.Ta#AR2]
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.SQL - select ta0_.ID_A as ID_A1_, ta0_.ID_B1 as ID_B11_, ta0_.ID_B2 as ID_B21_, tb1_.ID_B1 as ID_B10_, tb1_.ID_B2 as ID_B20_, tb1_.DESCR as DESCR0_ from TA ta0_, TB tb1_ where ta0_.ID_A=? and ta0_.ID_B1=tb1_.ID_B1(+) and ta0_.ID_B2=tb1_.ID_B2(+)
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2005-08-14 22:35:25,328 [main] DEBUG net.sf.hibernate.type.StringType - binding 'AR2' to parameter: 1
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - processing result set
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - returning null as column: ID_B10_
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - returning null as column: ID_B20_
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - result row: null, AR2
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - Initializing object from ResultSet: AR2
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - Hydrating entity: example.Ta#AR2
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - returning 'BR1' as column: ID_B11_
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - returning null as column: ID_B21_
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - done processing result set (1 rows)
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - total objects hydrated: 1
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.SessionImpl - resolving associations for [example.Ta#AR2]
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.SessionImpl - loading [example.Tb#null:null]
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.SessionImpl - attempting to resolve [example.Tb#null:null]
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.SessionImpl - object not resolved in any cache [example.Tb#null:null]
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Materializing entity: [example.Tb#null:null]
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.SQL - select tb0_.ID_B1 as ID_B10_, tb0_.ID_B2 as ID_B20_, tb0_.DESCR as DESCR0_ from TB tb0_ where tb0_.ID_B1=? and tb0_.ID_B2=?
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 1
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 2
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - processing result set
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - done processing result set (0 rows)
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2005-08-14 22:35:25,343 [main] DEBUG net.sf.hibernate.loader.Loader - total objects hydrated: 0
2005-08-14 22:35:25,359 [Finalizer] DEBUG net.sf.hibernate.impl.SessionImpl - running Session.finalize()
2005-08-14 22:35:25,359 [Finalizer] WARN net.sf.hibernate.impl.SessionImpl - unclosed connection