-->
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.  [ 11 posts ] 
Author Message
 Post subject: Simple Query issues
PostPosted: Mon Sep 01, 2003 7:09 pm 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
Hi all,

I have a very simple question about queries.

I am trying to run the following query:

from Student student where student.StudentId = " + query.getStudentId()

on the following class mapping:

<class
name="Student"
table="STUDENT"
proxy="Student"
dynamic-update="true"
>

<id
name="StudentId"
type="java.lang.String"
column="STUDENT_ID"
>
<generator class="assigned" />
</id>
<property
name="studentName"
type="java.lang.String"
column="STUDENT_NAME"
not-null="true"
length="100"
/>
<property
name="gender"
type="java.lang.String"
column="GENDER"
length="1"
/>
<property
name="grade"
type="java.lang.String"
column="GRADE"
length="10"
/>
</class>

When I run this query, I get a java.lang.ClassCastException. Am I missing anything here?

THanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 7:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Stack Trace??!


Top
 Profile  
 
 Post subject: Here is my stack trace...
PostPosted: Mon Sep 01, 2003 7:36 pm 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
norc.enum.util.ApplicationException: StudentDelegateImpl createStudent(): Something has gone wrong....java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: Problem creating Student DAOException: Problem hibernating....net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for Parent instance with identifier: 9
at delegate.impl.StudentDelegateImpl.createStudent(StudentDelegateImpl.java:71)
at delegate.test.StudentDelegateImplTest.testCreateStudent(StudentDelegateImplTest.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)


Top
 Profile  
 
 Post subject: Ooops..sorry wrong stack trace..This is the one..
PostPosted: Mon Sep 01, 2003 7:40 pm 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
ApplicationException: StudentDelegateImplSomething has gone wrong in searching Data....java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.ServerException: RuntimeException; nested exception is:
java.lang.ClassCastException
at delegate.impl.StudentDelegateImpl.searchData(StudentDelegateImpl.java:121)
at delegate.test.StudentDelegateImplTest.testSearchData(StudentDelegateImplTest.java:154)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You have not shown us the entire transaction! Please, you MUST provide everything between openSession() and close().

In this case, the problem is something to so with use of "assigned" identifier withunsaved-value="null".

Perhaps you can fix it by using unsaved-value="any".


But what you should do is: go and read the parent/child relationship page of the documentation TWICE before posting again.


Thanks in advance.


Top
 Profile  
 
 Post subject: My entire transaction
PostPosted: Tue Sep 02, 2003 1:24 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
String hQuery =
"fromStudent student where student.studentId = " + query.getStudentId();

try {
Session session = HibernateDAOFactory.getSession();
return session.find(hQuery);
} catch (HibernateException e) {
throw new DAOException("Problem in dealing with Hibernate" + e);
}

Based on my mapping above, when I run this query, I get the class exception with the stack trace I showed above. It seems like a simple transaction to test. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 1:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Two comments:
1) you need a space between from and Student.
2) Although it will work it is best if you use a parameter in the query rather then use the appended string. It allows opportunity for the prepared query to be cached (Hibernate and Database) and reduces object allocation. This helps performance on two levels.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 1:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
(1) you have shown two different stack traces. One of them is a problem to do with a wong unsaved-value mapping. The second shows an exception occuring in *application code*, not in Hibernate.

(2) the code you *did* show is obviously wrong: "fromStudent student where student.studentId = " is missing a space!

(3) the code you have shown has no cast in it, hence it is not where the class cast exception in the stack trace is coming from!


Hence I conclude that you probably havn't done enough work tracking down the actual source of the exception to be even posting here yet. I suggest you _step_though_your_own_code_ in the debugger to ascertain exactly where the CCE occurs, and then decide what the runtime class is that you are trying to cast to something its not.

This is just basic troubleshooting stuff!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 2:40 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
Gavin,

Thanks for your help so far. I am definitely trying to debugg this issue but have not been successful so far. In any case, in point 1 you are right on. But I immediately posted the correct stack track with an explanation in the subject. I thought that kinda explained the issue there. Point 2 is a copy/paste/simplify the code so the focus would be on the issue. There is a space in the the actual query.

Point 3 is what I had the question on. I have no cast in my code. It seems that Hibernate is doing some cast internally. Taking your last piece of advice, I stepped through my code again and dug pretty deep to find that the class net.sf.hibernate.type.IntegerType has a method:
set(PreparedStatement st, Object value, int index) that is called from the following stack of classes/methods:
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:46)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:31)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:494)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:136)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:587)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:42)

set is trying to cast the id I passed it, which is string, to an int.
public void set(PreparedStatement st, Object value, int index)
throws SQLException {
st.setInt(index, ((Integer) value).intValue());
}

The values of the arguments for value and index are "stID1" and index=1. This is where the ClassCastException is taking place:
((Integer) value).intValue() since value is "stID1". Now, given my mapping file and class which specifies that the id is string, why is Hibernate trying to cast the first column value in the resultset (which is has retrieved successfully) to an int when it is actually a String (varchar)? How does it come to think that id is int and not refer to either the mapping file, class, or the resultset metadata to infer that it is dealing with a String?

I would appreciate some more guidance on how to deal with this. Thanks.


Top
 Profile  
 
 Post subject: Another try..
PostPosted: Tue Sep 02, 2003 2:58 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
I also tried the following code:

String hQuery =
"from Student student where student.studentId =?";

try {
Session session = sessionFactory.openSession();;
Collection c = session.find(hQuery, "stID1", Hibernate.STRING);

Iterator it = c.iterator();

while(it.hasNext()){
System.out.println("Student found " + it.next());
}

} catch (HibernateException e) {
e.printStackTrace();
}

Still got the same stack trace with a CCE.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
(1) For future reference when I ask for a stack trace, I mean the WHOLE stack trace! ie. including the stack trace from inside Hibernate. If you swallow the stack trace in your application, it looks to me like the exception occurs in *your* code.

(2) You are not telling me the whole truth! This code:

session.find(hQuery);

ie. A query with no parameters NEVER results in a call to nullSafeSet(), unless there is some association or *something* that is being loaded recursively. However, your mapping document does not show any association. Perhaps you are implementing Lifecycle or something.


Quote:
why is Hibernate trying to cast the first column value in the resultset (which is has retrieved successfully) to an int when it is actually a String (varchar)



Hibernate is doing nothing of the sort! The call to set() is passed a PreparedStatement, not a ResultSet. This is all occurring in prepareQueryStatement, BEFORE a query is executed, and BEFORE there is any result set.

now look at this:

Code:
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:42)


ie. we are inside a *load()*, not a query!. What looks to be happening is that there is some association or something that you havn't told me about. So the first query has successfully executed and now something sparks a second query.


Now, what I need is the whole Hibernate stack trace. Where is the rest of the stack trace, ie. before EntityLoader.load?


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