-->
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.  [ 6 posts ] 
Author Message
 Post subject: Exception thrown when trying to load with primary key:String
PostPosted: Thu Apr 22, 2004 4:09 am 
Newbie

Joined: Sun Apr 11, 2004 9:38 am
Posts: 12
Hi,
I am using the Hibernate Session load() method to load an object. However, if the primary key is a String I get an exception.

Syntax of load method used:-
public Object load(Class theClass,Serializable id) throws HibernateException


The code is as follows:-

public void load(CommandInExecution cmdExec,BusinessEntity entity, Object primaryKey)
throws PersistenceException
{
Class classObj = entity.getEntityDefinition().getModelImplClass();
HibernateExecutionContext ctx = (HibernateExecutionContext) cmdExec;

Session sessionObj = ctx.getHibernateSession();

try
{
Object model = sessionObj.load(classObj,(Serializable)primaryKey);
entity.setState(model);
entity.setPrimaryKey(primaryKey);
}
catch (Exception ex)
{
ex.printStackTrace();
throw new PersistenceException(
this.toString(),
"load()",
"Unable to load the object of class:"
+ classObj
+ " from the database.",
ex);
}
}

I get the following exception on the line using the load function:-java.lang.ClassCastException: java.lang.String
at net.sf.hibernate.type.IntegerType.set(IntegerType.java:31)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:674)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:713)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:831)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:851)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2081)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1955)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1884)
at dolphin.framework.persistenceimpl.HibernateDBPersistenceManager.load(HibernateDBPersistenceManager.java:861)
at dolphin.framework.persistenceimpl.HibernateDBPersistenceManager.load(HibernateDBPersistenceManager.java:831)

Please let me know the reason.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 4:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well I suspect you have not mapped your primary key as a String, as Hibernate tries to use IntegerType ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 4:54 am 
Newbie

Joined: Sun Apr 11, 2004 9:38 am
Posts: 12
Hi,

I forgot to add the concerned part of the hbm file in my post. Here it is:-


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

This shows it is a String.
I face this problem only for this table.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 9:04 am 
Newbie

Joined: Sun Apr 11, 2004 9:38 am
Posts: 12
My problem is solved. Thanks and sorry.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 10:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well would be great to post how you solved it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:13 am 
Newbie

Joined: Sun Apr 11, 2004 9:38 am
Posts: 12
Nothing, passing the wrong type of serializable object to the load method.
I suppose a ClassCastException thrown would make it easier to catch the problem faster...?


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