-->
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.  [ 5 posts ] 
Author Message
 Post subject: Inheritance Mapping: How do I do this?
PostPosted: Mon Oct 20, 2008 6:56 pm 
Newbie

Joined: Fri Mar 21, 2008 1:27 pm
Posts: 9
Location: Los Angeles, US
Hibernate version:
3.4 using EntityManager

Mapping documents:
N/A

Code between sessionFactory.openSession() and session.close():
N/A, using Seam Managed Persistence

Full stack trace of any exception that occurs:
None

Name and version of the database you are using:
MySQL 5.0

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:
N/A

Problems with Session and transaction handling?
No

Code:
@Entity
@Table(name = "pet")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Pet {
....
}


Code:
@Entity
@DiscriminatorValue("1")
public class Dog extends Pet {
....
}


Code:
@Entity
@DiscriminatorValue("2")
public class Cat extends Pet {
...
}


Code:
@Entity
@Table(name="human")
public class Human {

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "pet_id", nullable = false)
  private Pet pet;
...
}


Code:
Human john = new Human();
john.setPet(new Cat());

Human mary = new Human();
mary.setPet(new Dog());


If I persist john and mary objects and retreive them from the database, Hibernate always returns Pet instances when I call john.getPet() or mary.getPet() and not Dog or Cat instances . What should I do to actually get the Dog/Cat instances? Is that even possible?

Thanks!

_________________
if you find yourself in a hole, stop digging.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 9:08 pm 
Newbie

Joined: Thu May 22, 2008 9:48 am
Posts: 13
Check instanceof for the returned objects from your getPet method. Of course it will return a Pet as that is what the method declares (I assume) in the abstract superclass. I'd imagine it can be cast into a subclass if you need to operate on it as a Dog/Cat. I'm not 100% about this, but it's my hunch.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 9:14 pm 
Newbie

Joined: Fri Mar 21, 2008 1:27 pm
Posts: 9
Location: Los Angeles, US
I already tried using instanceof/casting but the object that comes back from Hibernate is of type Pet (enhanced with JavaAssist/CGLIB) and cannot be cast to Dog or Cat.

_________________
if you find yourself in a hole, stop digging.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 2:18 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is expected when you use lazy loading as described by http://www.hibernate.org/hib_docs/v3/re ... ng-proxies


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 2:27 am 
Newbie

Joined: Fri Mar 21, 2008 1:27 pm
Posts: 9
Location: Los Angeles, US
I've already read the documentation. In the doc, they suggest to define new interfaces as a workaround and they only show standard Hibernate configuration. I just don't know how it would apply to JPA/EntityManager annotations.

Thanks!

_________________
if you find yourself in a hole, stop digging.


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