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.  [ 3 posts ] 
Author Message
 Post subject: Class base type
PostPosted: Sun Sep 18, 2005 5:10 am 
Newbie

Joined: Sun Sep 18, 2005 4:39 am
Posts: 3
The issue:

class A {}
class B extends A {}

class C {
public A clazzA;
}

There is mySQL table (one per hierarchy) that consists class A and class B types.

The heart of the problem that classC.clazzA may contain class B.

IMHO, it's all Ok, but Hibernate when loading class C, set the field clazzA in object of class A though the table have indicator (discriminator) that this concrete row contains class B ( ! )

i.e. objC.classA.getClass().getName() ==> class A (should be class B)

and consequence: B b = (B) objC.classA ==> ClassCastException

?


Top
 Profile  
 
 Post subject: Re: Class base type
PostPosted: Sun Sep 18, 2005 10:08 pm 
Newbie

Joined: Sun Sep 18, 2005 4:39 am
Posts: 3
May be we should talk about implisit polymorphism... ?


Top
 Profile  
 
 Post subject: Re: Class base type
PostPosted: Sun Sep 18, 2005 11:36 pm 
Newbie

Joined: Sun Sep 18, 2005 4:39 am
Posts: 3
Yo, guys, i found the problem!

So, I check "test" directory of Hibernate's sources.
There is FooBarTest.java that contains:


public void testPolymorphism() throws Exception {
Session s = openSession();
Bar bar = new Bar();
s.save(bar);
bar.setBarString("bar bar");
s.flush();
s.connection().commit();
s.close();
s = openSession();
FooProxy foo = (FooProxy) s.load( Foo.class, bar.getKey() );
assertTrue( "polymorphic", foo instanceof BarProxy );
assertTrue( "subclass property", ( (BarProxy) foo ).getBarString().equals( bar.getBarString() ) );
s.delete(foo);
s.flush();
s.connection().commit();
s.close();
}


I create the same test but I haven't used Proxy:


public void testGavPolymorphism() throws Exception {
Session s = openSession();
Bar bar = new Bar();
s.save(bar);
bar.setBarString("bar bar");
s.flush();
s.connection().commit();
s.close();
s = openSession();
Foo foo = (Foo) s.load( Foo.class, bar.getKey() );
assertTrue( "polymorphic", foo instanceof Bar );
assertTrue( "subclass property", ( (Bar) foo ).getBarString().equals( bar.getBarString() ) );
s.delete(foo);
s.flush();
s.connection().commit();
s.close();
}

testout
=======

testPolymorphism - Success
testGavPolymorphism - Error: java.lang.ClassCastException


Use the Source, Luke (C) Star Wars
;)


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