-->
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.  [ 7 posts ] 
Author Message
 Post subject: Please help, cant figure out what's wrong
PostPosted: Fri Mar 10, 2006 2:32 am 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
Hi, i've got a really wierd case .. Just cant figure out what's wrong .. Please help .. T__T

The code is as simple as this :
Code:
Am01 mstUser = (Am01) session.get(Am01.class, "ADEK");
System.out.println(mstUser);

And the resulted object of mstUser is null .. The table actually has the record with "ADEK" primary key ..
I've taken a look at the debug, the generated query runs fine in my query tool, the row exists..

I've tested hql on the same case :
Code:
Am01 mstUser = (Am01) session.createQuery("from Am01 A where A.userid='ADEK'").uniqueResult();
System.out.println(mstUser);

and it's result is not null and correct !

I really wonder what's wrong with the first code using session.get(Class,Serializabe) which results null

I've supplied my very simple confs and some debugs, please take a look :
Hibernate version:
3.1.beta2

Mapping documents:
excerp from hibernate.cfg.xml

<session-factory>

<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<property name="connection.url">jdbc:oracle:thin:@database:1521:TEST</property>
<property name="show_sql">true</property>

<mapping resource="test/model/Am01.hbm.xml" />
</session-factory>


full Am01.hbm.xml

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

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="test.model.Am01"
table="AM01"
>

<id
name="userid"
type="java.lang.String"
column="USERID"
>
<generator class="assigned" />
</id>

<property
name="usernm"
type="java.lang.String"
column="USERNM"
length="30"
/>
<property
name="passwd"
type="java.lang.String"
column="PASSWD"
length="20"
/>
<property
name="dflmnu"
type="java.lang.String"
column="DFLMNU"
length="10"
/>
<property
name="usracl"
type="java.lang.Integer"
column="USRACL"
length="1"
/>
<property
name="tglkey"
type="java.sql.Date"
column="TGLKEY"
length="7"
/>
<property
name="usrkey"
type="java.lang.String"
column="USRKEY"
length="10"
/>

<!-- associations -->

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Am01 mstUser = (Am01) session.get(Am01.class, "ADEK");
System.out.println(mstUser);


Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):
Hibernate: select am01x0_.USERID as USERID1_0_, am01x0_.USERNM as USERNM1_0_, am01x0_.PASSWD as PASSWD1_0_, am01x0_.DFLMNU as DFLMNU1_0_, am01x0_.USRACL as USRACL1_0_, am01x0_.TGLKEY as TGLKEY1_0_, am01x0_.USRKEY as USRKEY1_0_ from AM01 am01x0_ where am01x0_.USERID=?

Debug level Hibernate log excerpt:
13:36:00,875 DEBUG DefaultLoadEventListener:153 - loading entity: [test.model.Am01#ADEK]
13:36:00,876 DEBUG DefaultLoadEventListener:304 - attempting to resolve: [test.model.Am01#ADEK]
13:36:00,876 DEBUG DefaultLoadEventListener:340 - object not resolved in any cache: [test.model.Am01#ADEK]
13:36:00,876 DEBUG AbstractEntityPersister:2698 - Fetching entity: [test.model.Am01#ADEK]
13:36:00,877 DEBUG Loader:1774 - loading entity: [test.model.Am01#ADEK]
13:36:00,905 DEBUG AbstractBatcher:309 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
13:36:00,906 DEBUG SQL:344 - select am01x0_.USERID as USERID1_0_, am01x0_.USERNM as USERNM1_0_, am01x0_.PASSWD as PASSWD1_0_, am01x0_.DFLMNU as DFLMNU1_0_, am01x0_.USRACL as USRACL1_0_, am01x0_.TGLKEY as TGLKEY1_0_, am01x0_.USRKEY as USRKEY1_0_ from AM01 am01x0_ where am01x0_.USERID=?
Hibernate: select am01x0_.USERID as USERID1_0_, am01x0_.USERNM as USERNM1_0_, am01x0_.PASSWD as PASSWD1_0_, am01x0_.DFLMNU as DFLMNU1_0_, am01x0_.USRACL as USRACL1_0_, am01x0_.TGLKEY as TGLKEY1_0_, am01x0_.USRKEY as USRKEY1_0_ from AM01 am01x0_ where am01x0_.USERID=?
13:36:00,907 DEBUG AbstractBatcher:413 - preparing statement
13:36:00,985 DEBUG StringType:60 - binding 'ADEK' to parameter: 1
13:36:00,990 DEBUG AbstractBatcher:325 - about to open ResultSet (open ResultSets: 0, globally: 0)
13:36:00,991 DEBUG Loader:682 - processing result set
13:36:00,992 DEBUG Loader:709 - done processing result set (0 rows)
13:36:00,992 DEBUG AbstractBatcher:332 - about to close ResultSet (open ResultSets: 1, globally: 1)
13:36:00,992 DEBUG AbstractBatcher:317 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
13:36:00,993 DEBUG AbstractBatcher:459 - closing statement
13:36:00,995 DEBUG Loader:839 - total objects hydrated: 0
13:36:00,996 DEBUG StatefulPersistenceContext:780 - initializing non-lazy collections
13:36:00,996 DEBUG Loader:1805 - done entity load
null (the output of System.out.println)

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


Top
 Profile  
 
 Post subject: What is the DDL for your table?
PostPosted: Fri Mar 10, 2006 2:47 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
Could you also post the create table statement for the AM01 table?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 2:56 am 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
I was gonna put it in original post, but forgot ..
Anyway, here it is :

CREATE TABLE SOFCO.AM01 (
USERID CHAR(10) NOT NULL,
USERNM VARCHAR2(30) NULL,
PASSWD CHAR(20) NULL,
DFLMNU CHAR(10) NULL,
USRACL NUMBER(1,0) NULL,
TGLKEY DATE NULL,
USRKEY CHAR(10) NULL,
PRIMARY KEY(USERID)
)

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


Top
 Profile  
 
 Post subject: I think this has to do with char(10) declaration of the PK
PostPosted: Fri Mar 10, 2006 3:09 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
Your key is of type char(10), however you are passing only four characters. When you bind parameters (either implicitly through session.get() or explicitly through ':param' or '?' in a query, the length of the string must be equal to the size of the field. When you inline the string literal in the query (like in your query example) Oracle will 'pad' the constant for you, and return the right result. The varchar2 type does not have this problem - you can switch to it if you don't like the padding solution.

I hope this helps :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 3:18 am 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
That's very informative, thanks alot !

So, i cant just use session.get(Class, Serializable) on char type without padding the string to match the length of the field myself ? A very dilemmatic matter, since i cant just alter the table definition, legacy database T__T

Any other workarounds to make session.get() works besides padding the key manually and altering the table definition ?

(>'.')> <('.'<)

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


Top
 Profile  
 
 Post subject: Try using character type...
PostPosted: Fri Mar 10, 2006 4:01 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I am not certain that this would help, but you may try changing the mapping for id as follows:

<id name="userid" type="character" length="10" column="USERID">

Hibernate docs say that string is for varchar2, not char(n) mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 5:13 am 
Beginner
Beginner

Joined: Wed Feb 16, 2005 1:22 am
Posts: 25
Location: Jakarta
Hmm .. i've tried replacing String to character with length of 10, it didnt work ^___^

But i guess it's ok, i'm gonna use hql query instead of session.get() .. I just cant alter the legacy table that use char(n) key, could be disastrous considering it's used in a large project. Padding it manually to be used in session.get() works, but not a pretty option in our case.

Thank you, skalinic ! It's been fun .. hehe

_________________
Greater in battle
than the man who would conquer
a thousand-thousand men,
is he who would conquer
just one —
himself.


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