-->
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: ClassCastException
PostPosted: Wed Apr 20, 2005 11:35 pm 
Newbie

Joined: Wed Apr 20, 2005 10:38 pm
Posts: 2
What I am reporting here is either a bug or as designed, in either case I believe the situation can be improved.

The situation occurs when I map a heirachy of Group objects. These Group objects have subclasses FilterGroup and Mediator Group.

The mapping is as follows.

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 package="com.micromuse.smconfig.core.nodegrouping">
<class discriminator-value="group" name="Group" table="NODE_GROUP">

  <id column="ID" name="id" type="long">
   <generator class="increment"/>
  </id>
  <discriminator column="CLASS" force="false" insert="true"/>
  <version column="VERSION" name="version" type="long"/>
  <property name="guid" type="string">
   <column length="64" name="GUID"/>
  </property>
  <property name="name" type="string">
   <column length="255" name="NAME"/>
  </property>

  <many-to-one class="Group" column="PARENT_ID" name="parentGroup" cascade="none" lazy="false" />

  <set cascade="all-delete-orphan" inverse="true" lazy="false"
   name="childGroups" order-by="NAME asc" table="GROUPS">
   <key column="PARENT_ID"/>
   <one-to-many class="Group"/>
  </set>

  <subclass discriminator-value="filter" name="FilterGroup" >
   <many-to-one cascade="all" class="ExpressionRoot"
    column="EXPRESSION_ID" name="expressionRoot"/>
  </subclass>

  <subclass discriminator-value="mediator" name="MediatorGroup" >
   <many-to-one class="Mediator"
    column="MEDIATOR_ID" name="mediator"/>
  </subclass>

</class>
</hibernate-mapping>


Then I wrote some test code for the mapped class as follows

Code:
try
{
    Session session = _sessionFactory.openSession();
    Transaction trans = session.beginTransaction();
   
    FilterGroup group = new FilterGroup();
    group.setName("Parent");

    FilterGroup child = new FilterGroup();
    child.setName("Child");
    HashSet children = new HashSet();
    children.add(child);
          
    group.setChildGroups(children);
    child.setParentGroup(group);          
          
    session.save(group);
    session.save(child);
           
    Long id = group.getId();
    Long cid = child.getId();
    trans.commit();
    session.close();
    group = null;
           
    session = _sessionFactory.openSession();
    trans = session.beginTransaction();
    Group childtop = (Group) session.createQuery("from Group WHERE id=:id").setLong("id",cid.longValue()).list().get(0);

    child = (FilterGroup)childtop;

    Group grp = (Group)childtop.getParentGroup();
    [color=red]group = (FilterGroup)grp; [/color]
           
    session.delete(group);
    trans.commit();
    session.close();
}
catch (Exception e)
{
    fail("Failed in a persistance operation " + e);
}


The above code causes a ClassCastException on the highlighted command above. I find this unusual as I was able to cast the retrieved child to FilterGroup without any problems.

I also tried retrieving the parent first then getting the child then getting the parent from the child. That worked.

It looks to me like the proxied parentGroup has been proxied from Group not FilterGroup and so we can't cast it. Isn't it possible for the parent relationship to proxy the correct subclass?


Top
 Profile  
 
 Post subject: Re: ClassCastException
PostPosted: Thu Apr 21, 2005 12:09 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
micks128 wrote:
What I am reporting here is either a bug or as designed, in either case I believe the situation can be improved.

The situation occurs when I map a heirachy of Group objects. These Group objects have subclasses FilterGroup and Mediator Group.

.... code removed...

The above code causes a ClassCastException on the highlighted command above. I find this unusual as I was able to cast the retrieved child to FilterGroup without any problems.

I also tried retrieving the parent first then getting the child then getting the parent from the child. That worked.

It looks to me like the proxied parentGroup has been proxied from Group not FilterGroup and so we can't cast it. Isn't it possible for the parent relationship to proxy the correct subclass?



Not sure why but the proxy behavior appears to be inconsistent.

I would expect the lazy="false" attribute to eliminate the proxy for this attribute but it doesn't. However, using the top-level default-lazy="false", eliminated the proxying of the parentGroup object and the ClassCastException.

<hibernate-mapping package="com.micromuse.smconfig.core.nodegrouping" default-lazy="false">


Top
 Profile  
 
 Post subject: Lazy Loading
PostPosted: Thu Apr 21, 2005 8:13 pm 
Newbie

Joined: Wed Apr 20, 2005 10:38 pm
Posts: 2
With a heirachical mapping I believe it is a bad idea to turn off lazy loading. It would mean that loading a single item in the tree would load the rest of the tree.

BTW I forgot to mention in the last post my environment

I'm using

hibernate 3.1 maintainence release.
mysql 4.1
java 1.5.0_02

The result occurs I'm running the given code as a testcase


Top
 Profile  
 
 Post subject: Re: Lazy Loading
PostPosted: Thu Apr 21, 2005 8:39 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
micks128 wrote:
With a heirachical mapping I believe it is a bad idea to turn off lazy loading. It would mean that loading a single item in the tree would load the rest of the tree.

BTW I forgot to mention in the last post my environment

I'm using

hibernate 3.1 maintainence release.
mysql 4.1
java 1.5.0_02

The result occurs I'm running the given code as a testcase



I agree with you about the turning off lazy loading being a bad idea. I was merely trying to point out the different behavior between turning it off at the top-level versus turning it off for the individual mapping as you have for the <many-to-one> and <set> mappings in your example.

(and I assume you're using Hibernate 3.0.1 ?)


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.