-->
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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Iterating & casting after query
PostPosted: Wed Jun 07, 2006 4:05 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
Hibernate 3.1 beta 3
JBoss 4.0.3

I am having trouble while iterating over a List of results. I get a ClassCastException when I try to cast the proxy to User. Type found in the collection is com.monkeyden.pipeline.poc.bo.User$$EnhancerByCGLIB$$646a18c4

Code:
Query query = session.createQuery("from com.monkeyden.pipeline.poc.bo.User u where " +
                    "lower(u.email) like lower(:email)");
Iterator users = query.iterate();
while(users.hasNext()){
    Object current = users.next();
    User current = (User)current;
    results.add(current);
}


Code:
<class name="com.monkeyden.pipeline.poc.bo.User" table="USERS">
    <id name="id" type="java.lang.Long">
        <column name="USER_ID" not-null="true" sql-type="NUMBER" />
        <generator class="native">
            <!-- 
                To add non XDoclet generator parameters, create a file named
                hibernate-generator-params-User.xml
                containing the additional parameters and place it in your merge dir.
            -->
        </generator>
    </id>
    <property name="createDate" type="java.util.Date" update="true" insert="true">
        <column name="CREATE_DATE" not-null="true" sql-type="DATE" />
    </property>
    <property name="displayName" type="java.lang.String" update="true" insert="true">
        <column name="DISPLAY_NAME" not-null="true" sql-type="VARCHAR2" />
    </property>
    <property name="email" type="java.lang.String" update="true" insert="true">
        <column name="EMAIL" not-null="true" sql-type="VARCHAR2" />
    </property>
    <property name="password" type="java.lang.String" update="true" insert="true">
        <column name="PASSWORD" not-null="true" sql-type="VARCHAR2" />
    </property>
    <property name="username" type="java.lang.String" update="true" insert="true">
        <column name="USERNAME" not-null="true" sql-type="VARCHAR2" />
    </property>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 4:47 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
The proxy is an actual subclass of your class, which means you should be able to cast it.

Are you sure, though, that the code you posted is the code that is run?

You cannot redefine a variable in the same context, so this should not compile:

while(users.hasNext()){
Object current = users.next();
User current = (User)current;
results.add(current);
}

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:01 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
szroland wrote:
The proxy is an actual subclass of your class, which means you should be able to cast it.

Are you sure, though, that the code you posted is the code that is run?

You cannot redefine a variable in the same context, so this should not compile:

while(users.hasNext()){
Object current = users.next();
User current = (User)current;
results.add(current);
}

Roland


Sorry, copy and paste flub. Here is the actual code:

Code:
Query query = session.createQuery("from com.monkeyden.pipeline.poc.bo.User u where " +
                    "lower(u.email) like lower(:email)");

query.setParameter("email", email);

Iterator users = query.iterate();
while(users.hasNext()){
    Object row = users.next();
    User current = (User)row;
    results.add(current);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:30 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
Could you add these four lines, and show the result, including the exception itself?

while(users.hasNext()){
Object row = users.next();
System.out.println(row.getClass());
System.out.println(row.getClass().getGenericSuperclass());
System.out.println(User.class);
System.out.println(row instanceof User);


User current = (User)row;
results.add(current);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:39 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
The output was:

class com.nemoves.pipeline.poc.bo.User$$EnhancerByCGLIB$$71e1d5b6
class com.nemoves.pipeline.poc.bo.User
false

This has as much information as any RuntimeException, so I figured it wasn't worth putting in the initial post. Thanks
Code:
java.lang.ClassCastException
at com.monkeyden.pipeline.action.SimpleSearchAction.doSubmit(SimpleSearchAction.java:80)
at com.monkeyden.pipeline.admin.PipelineRequestProcessor.processActionPerform(PipelineRequestProcessor.java:49)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.monkeyden.pipeline.servlets.HibernateFilter.doFilter(HibernateFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:534)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Is User a final class, or does it contain any final methods or members? If it does, remove all instances of the "final" keyword and try it again..

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:08 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
monkeyden wrote:
The output was:
class com.nemoves.pipeline.poc.bo.User$$EnhancerByCGLIB$$71e1d5b6
class com.nemoves.pipeline.poc.bo.User
false


Why was the ouput only 3 lines, instead of 4?

My last idea is that the User class you imported in your SimpleSearchAction class, is not com.nemoves.pipeline.poc.bo.User but from some other package. Now you only have 3 lines of output, so I don't know if com.nemoves.pipeline.poc.bo.User is the ancestor class of the proxy (which it should be), or the actually imported User class you are trying to cast to (User.class).

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:09 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
You didn't somehow import a different class named "User" from a different package by accident, or anything like that? That would cause the same problem. I know it's a long shot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:10 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
The class is public and has a public no-arg and doesn't have any final data members. Is there anything I can check in the proxy instance that will shed some light on this? I can dig around in the debugger. I didn't see anything that stood out.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:21 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
Quote:
Why was the ouput only 3 lines, instead of 4?

Because getGenericSuperclass() is undefined for type class.

EDIT: Just realized it's available in Java 5. I'm using java 1.4

Quote:
My last idea is that the User class you imported in your SimpleSearchAction class, is not com.nemoves.pipeline.poc.bo.User but from some other package. Now you only have 3 lines of output, so I don't know if com.nemoves.pipeline.poc.bo.User is the ancestor class of the proxy (which it should be), or the actually imported User class you are trying to cast to (User.class).

You'll notice that in previous messages in this thread it has com.monkeyden for the package, which was an obfuscation attempt I grew tired of, but I assure you they are both in com.nemoves.pipeline.poc.bo. That was one of the first things I checked, since we have several projects with a User class. This is proven by the system output.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to verify that the proxy class is extending the correct User, you could use code like this:
Code:
while (users.hasNext()) {
  Object row = users.next();
  int i = 0;
  for (Object o = row; o.getClass() != Object.class; o = o.getSuperclass())
  {
    System.out.println("Ancestor " + i++ + " has class " + o.getClass().getName());
  }

  User current = (User)row;
  results.add(current);
}
Obviously, "Ancestor 0" will be the class itself.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:39 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
monkeyden wrote:
Quote:
Why was the ouput only 3 lines, instead of 4?

Because getGenericSuperclass() is undefined for type class.


Yes, sorry, that'd JDK 5. You could try getSuperclass() instead.

Otherwise, I'm really out of ideas. The proxy should be a subclass, I see no reason why it is not so (except your class being final, but that should generate an error early on I think). Sorry I couldn't help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:41 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
Object.getSuperclass() is also not available.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:43 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
monkeyden wrote:
Object.getSuperclass() is also not available.


It's a method of Class. So you would go like this: object.getClass().getSuperclass().

Or:
for (Class o = row.getClass(); o != Object.class; o = o.getSuperclass()) {
System.out.println(o);
}


Last edited by szroland on Wed Jun 07, 2006 6:57 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:46 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
I know JDO has a detachable attribute for the class. Does Hibernate have such an attribute? I didnt see anything related in the DTD.

Thanks for the sustained effort.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page 1, 2  Next

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.