-->
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: Hibernate/CGLIB weirdness
PostPosted: Tue Apr 11, 2006 3:29 pm 
Newbie

Joined: Tue Apr 11, 2006 3:00 pm
Posts: 4
I ran into a problem with one class/entity, called Role. I could not retrieve it by property using either query or criteria - the query returns empty set. I can retrieve an object by ID, but the stuff the query returns looks like Role$$EnhancerByCGLIB$$f45a5802 in the debugger (Eclipse), with object properties all nulls. Trying to call any methods on it causes ClassCastExceptions. The rest of the entities work fine and under debugger, look like regular classes - User, Department etc.

The class is completely trivial POJO mapped to a table, with no relationships to other entities.
SQL generated by Hibernate for query or criteria looks correct, and I am able to execute it directly and get expected results.

Any pointers would be much appreciated. I am out of ideas here...


Hibernate version: 3.0.2

Mapping documents: (generated by XDoclet)

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class
        name="com.project.model3.Role"
        table="role"
    >
        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
            not-null="true"
        />

        <property
            name="description"
            type="java.lang.String"
            update="true"
            insert="true"
            column="description"
        />

    </class>

</hibernate-mapping>


Full stack trace :
Code:
   
    [junit] INFO  - closing
    [junit] INFO  - cleaning up connection pool: jdbc:mysql://localhost/almond3
    [junit] ------------- ---------------- ---------------
    [junit] Testcase: testPopulate(com.project.test.Populate):  Caused an ERROR
    [junit] null
    [junit] java.lang.ClassCastException
    [junit]     at org.hibernate.type.LongType.set(LongType.java:40)
    [junit]     at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
    [junit]     at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
    [junit]     at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1078)
    [junit]     at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1140)
    [junit]     at org.hibernate.loader.Loader.doQuery(Loader.java:369)
    [junit]     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
    [junit]     at org.hibernate.loader.Loader.loadEntity(Loader.java:1307)
    [junit]     at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:141)
    [junit]     at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:126)
    [junit]     at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2461)
    [junit]     at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:392)
    [junit]     at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:373)
    [junit]     at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:155)
    [junit]     at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:117)
    [junit]     at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:593)
    [junit]     at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
    [junit]     at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
    [junit]     at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
    [junit]     at com.project.model3.Role$$EnhancerByCGLIB$$7707cc54.toString(<generated>)
    [junit]     at java.lang.String.valueOf(String.java:2131)
    [junit]     at java.lang.StringBuffer.append(StringBuffer.java:370)
    [junit]     at com.project.dao.UserOrgRoleDAO.createUserOrgRole(UserOrgRoleDAO.java:24)
    [junit]     at com.project.test.Populate.associateUsersWithRoles(Populate.java:202)
    [junit]     at com.project.test.Populate.testPopulate(Populate.java:41)

    [junit] Test com.project.test.Populate FAILED

Name and version of the database you are using:

The generated SQL (show_sql=true):
Code:
[junit] Hibernate: select role0_.id as id0_, role0_.name as name22_0_, role0_.description as descript3_22_0_ from role role0_ where role0_.id=?



Debug level Hibernate log excerpt:
I don't see anything suspicious in Hibernate logs.


Top
 Profile  
 
 Post subject: Type error
PostPosted: Wed Apr 12, 2006 5:13 am 
Newbie

Joined: Tue Apr 11, 2006 12:16 pm
Posts: 2
Hi.
In the properties "type" field you must define the hibernate equivalent type, not the Java type. It's not necessary to define String types in the mapping file for the String attributes. The same thing for "id" (if it's a java.lang.Long, hibernate manages the correct conversion).
Just remove type tags in your example.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 10:50 am 
Newbie

Joined: Tue Apr 11, 2006 3:00 pm
Posts: 4
The mappings are generated by XDoclet, and I did not originally specify the types. So it is XDoclet that reflects the types from the source code and places them in the mapping document.

If I change XDoclet tags in the Java class to include Hibernate types, the generated mapping file looks like the one below (type="java.lang.Long" is replaced with type="long"). Thye query breaks the same way - I get ClassCastException with identical stack trace.


Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="com.revolent.almond.model3.Role"
        table="role"
    >

        <id
            name="id"
            column="id"
            type="long"
        >
            </generator>
        </id>

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="name"
            not-null="true"
        />

        <property
            name="description"
            type="string"
            update="true"
            insert="true"
            column="description"
        />

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 4:56 pm 
Newbie

Joined: Tue Apr 11, 2006 3:00 pm
Posts: 4
Apparently, the cause is that I have multiple classes mapped to the same table, and I was coding to classes, not to interfaces. Due to the way proxies are generated for polymorphic classes, the instances of the objects were not what I was expecting.

I found a pointer to relevant part of the documentation(
http://www.hibernate.org/hib_docs/refer ... ce-proxies) by searching for EnhancerByCGLIB and ClassCastException.

I must say I am still uncomfortable with the idea of bytecode manipulation...


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.