-->
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.  [ 4 posts ] 
Author Message
 Post subject: Query.list() returns one item which is null
PostPosted: Fri Sep 29, 2006 8:09 am 
Newbie

Joined: Tue Sep 05, 2006 11:11 am
Posts: 11
I have a query that correctly finds one record that matches the query, but the value that is returned is null. Running the same query in sqlplus returns shows the expected record. By removing the composite key and making dataId be the key the problem goes away, however, I like to understand why. Any explanation would be appreciated.

BTW, I have rewritten the method using criteria and hibernate is unable to resolve the owningSystem regardless of whether I use the name or column. With the compoiste key removed that works fine too.

Hibernate version: 3.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.gxs.iefe.alias">
<class
name="AliasTable"
table="IEFE.ALIAS_TABLE"
>
<meta attribute="sync-DAO">false</meta>
<composite-id name="id" class="AliasTablePK">
<key-property
name="owningSystem"
column="OWNING_SYSTEM"
type="string"
/>
<key-property
name="owningAccount"
column="OWNING_ACCOUNT"
type="string"
/>
<key-property
name="owningUserId"
column="OWNING_USER_ID"
type="string"
/>
<key-property
name="tableName"
column="TABLE_NAME"
type="string"
/>
</composite-id>

<property
name="updatedTime"
column="UPDATED_TIME"
type="date"
not-null="false"
length="7"
/>
<property
name="updatedAccount"
column="UPDATED_ACCOUNT"
type="string"
not-null="false"
length="8"
/>
<property
name="updatedUserId"
column="UPDATED_USER_ID"
type="string"
not-null="false"
length="8"
/>
<property
name="dataId"
column="DATA_ID"
type="long"
not-null="false"
length="22"
/>


</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():


private AliasTable findAliasTableRecord(
IeAddress theOwner,
String theTable,
Session theSession
) throws Exception
{
if ( null == theOwner )
{
throw new Exception( "theOwner is null!" );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"{ AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() );

Query q = null;
AliasTable anAliasTable = null;

// The system is always present, however, the account and user ID may not
// be present for global and organizational tables.
if ( null != theOwner.getAccount() && ! "".equals( theOwner.getAccount() ))
{
if ( null != theOwner.getUserId() && ! "".equals( theOwner.getUserId() ) )
{
q = theSession.createQuery(
"from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT = :ac and OWNING_USER_ID = :user and TABLE_NAME = :tbl" );
q.setString( "sys", theOwner.getSystem() );
q.setString( "ac", theOwner.getAccount() );
q.setString( "user", theOwner.getUserId() );
q.setString( "tbl", theTable );
}
else
{
q = theSession.createQuery(
"from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT = :ac and OWNING_ACCOUNT is null and TABLE_NAME = :tbl" );
q.setString( "sys", theOwner.getSystem() );
q.setString( "ac", theOwner.getAccount() );
q.setString( "tbl", theTable );
}
}
else
{
q = theSession.createQuery(
"from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl" );
q.setString( "sys", theOwner.getSystem() );
q.setString( "tbl", theTable );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord", q.toString() );

// Retrieve the data
List anAliasTableList = q.list();

if ( null == anAliasTableList || 0 == anAliasTableList.size() )
{
throw new Exception(
"AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() +
" was not found!" );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"AliasTableList.size: " + anAliasTableList.size() +
" AliasTableList: " + anAliasTableList.toString() );

if ( 1 < anAliasTableList.size() )
{
throw new Exception(
"Found more than 1 match for AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() );
}

anAliasTable = (AliasTable)anAliasTableList.get( 0 );

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"} AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() +
" was" + ( null == anAliasTable ? " not " : " " ) + "found." );

return anAliasTable;
}


private AliasTable findAliasTableRecord3(
IeAddress theOwner,
String theTable,
Session theSession
) throws Exception
{
if ( null == theOwner )
{
throw new Exception( "theOwner is null!" );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"{ AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() );

Criteria aCriteria = theSession.createCriteria( AliasTable.class );

AliasTable anAliasTable = null;

// The system is always present, however, the account and user ID may not
// be present for global and organizational tables.
if ( null != theOwner.getAccount() && ! "".equals( theOwner.getAccount() ))
{
if ( null != theOwner.getUserId() && ! "".equals( theOwner.getUserId() ) )
{
aCriteria.add( Restrictions.eq( "owningSystem", theOwner.getSystem() ) );
aCriteria.add( Restrictions.eq( "owningAccount", theOwner.getAccount() ) );
aCriteria.add( Restrictions.eq( "owningUserId", theOwner.getUserId() ) );
aCriteria.add( Restrictions.eq( "tableName", theTable ) );
}
else
{
aCriteria.add( Restrictions.eq( "owningSystem", theOwner.getSystem() ) );
aCriteria.add( Restrictions.eq( "owningAccount", theOwner.getAccount() ) );
aCriteria.add( Restrictions.isNull( "owningUserId" ) );
aCriteria.add( Restrictions.eq( "tableName", theTable ) );
}
}
else
{
//aCriteria.add( Restrictions.eq( "OWNING_SYSTEM", theOwner.getSystem() ) );
aCriteria.add( Restrictions.eq( "owningSystem", theOwner.getSystem() ) );
aCriteria.add( Restrictions.isNull( "owningAccount" ) );
aCriteria.add( Restrictions.isNull( "owningUserId" ) );
aCriteria.add( Restrictions.eq( "tableName", theTable ) );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
aCriteria.toString() );

// Retrieve the data
List anAliasTableList = aCriteria.list();

if ( null == anAliasTableList || 0 == anAliasTableList.size() )
{
throw new Exception(
"AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() +
" was not found!" );
}

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"AliasTableList.size: " + anAliasTableList.size() +
" AliasTableList: " + anAliasTableList.toString() );

if ( 1 < anAliasTableList.size() )
{
throw new Exception(
"Found more than 1 match for AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() );
}

anAliasTable = (AliasTable)anAliasTableList.get( 0 );

Trace.trace( TraceLevel.DEBUG, this, "findAliasTableRecord",
"} AliasTable: " + theTable + " owner: " +
theOwner.getSystem() + "." +
theOwner.getAccount() + "." +
theOwner.getUserId() +
" was" + ( null == anAliasTable ? " not " : " " ) + "found." );

return anAliasTable;
}



Full stack trace of any exception that occurs:
My code generates one because one object is returned but that object is null.


Name and version of the database you are using: Oracle 9

The generated SQL (show_sql=true):
select
aliastable0_.OWNING_SYSTEM as OWNING1_0_,
aliastable0_.OWNING_ACCOUNT as OWNING2_0_,
aliastable0_.OWNING_USER_ID as OWNING3_0_,
aliastable0_.TABLE_NAME as TABLE4_0_,
aliastable0_.UPDATED_TIME as UPDATED5_0_,
aliastable0_.UPDATED_ACCOUNT as UPDATED6_0_,
aliastable0_.UPDATED_USER_ID as UPDATED7_0_,
aliastable0_.DATA_ID as DATA8_0_
from
IEFE.ALIAS_TABLE aliastable0_
where
OWNING_SYSTEM=?
and (
OWNING_ACCOUNT is null
)
and (
OWNING_USER_ID is null
)
and TABLE_NAME=?


Debug level Hibernate log excerpt:
20060929 08:00:26,814 Thread-0 throwQueryException() : no errors
20060929 08:00:26,824 Thread-0 HQL: from com.gxs.iefe.alias.AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl
20060929 08:00:26,824 Thread-0 SQL: select aliastable0_.OWNING_SYSTEM as OWNING1_0_, aliastable0_.OWNING_ACCOUNT as OWNING2_0_, aliastable0_.OWNING_USER_ID as OWNING3_0_, aliastable0_.TABLE_NAME as TABLE4_0_, aliastable0_.UPDATED_TIME as UPDATED5_0_, aliastable0_.UPDATED_ACCOUNT as UPDATED6_0_, aliastable0_.UPDATED_USER_ID as UPDATED7_0_, aliastable0_.DATA_ID as DATA8_0_ from IEFE.ALIAS_TABLE aliastable0_ where OWNING_SYSTEM=? and (OWNING_ACCOUNT is null) and (OWNING_USER_ID is null) and TABLE_NAME=?
20060929 08:00:26,824 Thread-0 throwQueryException() : no errors
20060929 08:00:26,854 Thread-0 HQL param location recognition took 20 mills (from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl)
20060929 08:00:26,884 Thread-0 (10) com.gxs.iefe.alias.Alias:findAliasTableRecord QueryImpl(from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl)
20060929 08:00:26,884 Thread-0 located HQL query plan in cache (from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl)
20060929 08:00:26,884 Thread-0 find: from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl
20060929 08:00:26,884 Thread-0 named parameters: {sys=D47, tbl=G48}
20060929 08:00:26,884 Thread-0 about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
20060929 08:00:26,884 Thread-0
select
aliastable0_.OWNING_SYSTEM as OWNING1_0_,
aliastable0_.OWNING_ACCOUNT as OWNING2_0_,
aliastable0_.OWNING_USER_ID as OWNING3_0_,
aliastable0_.TABLE_NAME as TABLE4_0_,
aliastable0_.UPDATED_TIME as UPDATED5_0_,
aliastable0_.UPDATED_ACCOUNT as UPDATED6_0_,
aliastable0_.UPDATED_USER_ID as UPDATED7_0_,
aliastable0_.DATA_ID as DATA8_0_
from
IEFE.ALIAS_TABLE aliastable0_
where
OWNING_SYSTEM=?
and (
OWNING_ACCOUNT is null
)
and (
OWNING_USER_ID is null
)
and TABLE_NAME=?
Hibernate:
select
aliastable0_.OWNING_SYSTEM as OWNING1_0_,
aliastable0_.OWNING_ACCOUNT as OWNING2_0_,
aliastable0_.OWNING_USER_ID as OWNING3_0_,
aliastable0_.TABLE_NAME as TABLE4_0_,
aliastable0_.UPDATED_TIME as UPDATED5_0_,
aliastable0_.UPDATED_ACCOUNT as UPDATED6_0_,
aliastable0_.UPDATED_USER_ID as UPDATED7_0_,
aliastable0_.DATA_ID as DATA8_0_
from
IEFE.ALIAS_TABLE aliastable0_
where
OWNING_SYSTEM=?
and (
OWNING_ACCOUNT is null
)
and (
OWNING_USER_ID is null
)
and TABLE_NAME=?
20060929 08:00:26,884 Thread-0 preparing statement
20060929 08:00:26,894 Thread-0 bindNamedParameters() D47 -> sys [1]
20060929 08:00:26,894 Thread-0 bindNamedParameters() G48 -> tbl [2]
20060929 08:00:27,234 Thread-0 about to open ResultSet (open ResultSets: 0, globally: 0)
20060929 08:00:27,234 Thread-0 processing result set
20060929 08:00:27,234 Thread-0 result set row: 0
20060929 08:00:27,234 Thread-0 result row: null
20060929 08:00:27,335 Thread-0 done processing result set (1 rows)
20060929 08:00:27,335 Thread-0 about to close ResultSet (open ResultSets: 1, globally: 1)
20060929 08:00:27,335 Thread-0 about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
20060929 08:00:27,335 Thread-0 closing statement
20060929 08:00:27,425 Thread-0 total objects hydrated: 0
20060929 08:00:27,425 Thread-0 initializing non-lazy collections
20060929 08:00:27,425 Thread-0 located HQL query plan in cache (from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl)
20060929 08:00:27,425 Thread-0 find: from AliasTable where OWNING_SYSTEM = :sys and OWNING_ACCOUNT is null and OWNING_USER_ID is null and TABLE_NAME = :tbl
20060929 08:00:27,425 Thread-0 named parameters: {sys=D47, tbl=G48}
20060929 08:00:27,425 Thread-0 about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
20060929 08:00:27,435 Thread-0
select
aliastable0_.OWNING_SYSTEM as OWNING1_0_,
aliastable0_.OWNING_ACCOUNT as OWNING2_0_,
aliastable0_.OWNING_USER_ID as OWNING3_0_,
aliastable0_.TABLE_NAME as TABLE4_0_,
aliastable0_.UPDATED_TIME as UPDATED5_0_,
aliastable0_.UPDATED_ACCOUNT as UPDATED6_0_,
aliastable0_.UPDATED_USER_ID as UPDATED7_0_,
aliastable0_.DATA_ID as DATA8_0_
from
IEFE.ALIAS_TABLE aliastable0_
where
OWNING_SYSTEM=?
and (
OWNING_ACCOUNT is null
)
and (
OWNING_USER_ID is null
)
and TABLE_NAME=?



Problems with Session and transaction handling? No


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 10:58 am 
Newbie

Joined: Fri Oct 06, 2006 9:13 am
Posts: 1
I think I've encountered the same issue. I believe the problem is that one of the columns defined to Hibernate as part of the composite key has a null value. I did a (very) little tracing and it seems that after the data is fetched the ComponentType object tries to extract a key value from the result set. If it encounters a null in any column it gives up. The problem went away for me when I made sure all fields defined as part of the composite key had data in all rows.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 2:39 pm 
Newbie

Joined: Tue Sep 05, 2006 11:11 am
Posts: 11
I think you are right on. In my case, one of the fields in the composite key is NULL, but it is okay for that field to be NULL sometimes.

I read that use of composite-key is not recommended so I ended up rewriting the object to use dataId as the primary key and the problems went away.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 23, 2006 12:37 pm 
Newbie

Joined: Tue Sep 05, 2006 11:11 am
Posts: 11
I ran into this problem with another class. That class requires the use of composite-key. To resolve the issue, load() was changed to get().


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