-->
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: ClassCast for similar objects
PostPosted: Thu Sep 21, 2006 5:55 am 
Beginner
Beginner

Joined: Thu Sep 21, 2006 2:49 am
Posts: 24
ClassCast for similar objects

Hibernate version: 3.2.0 CR4 with Annotationen

DB: MySQL


I hava the following class hirarchie (using Table per subclass strategie):
MethaBean extends Object
Person extends Metha Bean
Employee extends Person
User extends Person

Depending on the context the same person can be a User or an Employee. how to do the cast?


The following code prints always an employee, because I created it as an employee. but how can I convert (cast) it to an User or a Person.
(The second object gets a ClasCastException, of course)

Code:
Session session = SessionFactory.currentSession();
        Long lID = new Long(1030);
        Object bean1 = null;
        Object bean2 = null;
        Object bean3 = null;
        try
        {
            bean1 = (Person)session.get(Person.class, lID);
            bean3 = (Employee) session.get(Employee.class, lID);
            bean2 = (User) session.get(User.class, lID);
           
        }
        catch (ClassCastException e)
        {
            e.printStackTrace();
        }
        System.out.println("1 Person :" + bean1);
        System.out.println("2 User :" + bean2);
        System.out.println("3 Employee :" + bean3);



Results:
Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
[2006.09.21][com.co.util.hibernate.SessionFactory][][DEBUG]: hibernate SessionFactory created by
[2006.09.21][com.co.util.hibernate.SessionFactory][][DEBUG]: hibernate session opened by
java.lang.ClassCastException: com.co.util.beans.Employee
at com.co.test.HibernateTest.main(HibernateTest.java:39)
1 Person :co.util.beans.Employee: Wasle Florian (1030)
2 User :null
3 Employee :co.util.beans.Employee: Wasle Florian (1030)


Thanks,
Florian

_________________
http://www.dooris.de


Last edited by florian79 on Thu Sep 21, 2006 2:23 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 11:31 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Wll forget about hibernate!! how could you do that in plain Java?
can you change class of an object?!

Hibenate fetches an object from database, what class should it be? User or Employee? when your object can be both!

Maybe you must refactor your code, for example "Employee extends User" or something like this

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 2:15 pm 
Beginner
Beginner

Joined: Thu Sep 21, 2006 2:49 am
Posts: 24
Yes I want to fetch the data from datbase and crete an object using this data. It should be possible to get the object as Person, User OR Employee. The session context just binds one object either a Person or an User, of course. And the next time with a new session I want to fetch it as another object.

It is not possible in plain java because if I cast from Employee to User the Employee-specific data would get lost. But that could not happen with o/r-mapping - there we have diferent database tables for each object.

I think that would be a nice feacher!

_________________
http://www.dooris.de


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.