-->
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: Can an entity inherit from another inherited entity?
PostPosted: Sat Jun 14, 2008 3:07 am 
Newbie

Joined: Fri Feb 29, 2008 7:05 am
Posts: 13
Location: New Delhi, India
Hi All,

I am working on a Hibernate application and using annotations to define entities in the system. I am now facing a issue in which I am not very clear if the solution I have in mind is possible or not.

I would like to know if it is possible to inherit an entity from an already inherited entity? My case is described with the help of following code snippet:

Code:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SuperClass{

}

@Entity
@DiscriminatorValue("test")
public class InheritedClass extends SuperClass{

}

@Entity
@DiscriminatorValue("test11")
public class InheritedClass2 extends InheritedClass{

}


I want to create the InheritedClass2. But when I save this entity, I am getting an exception.

Please let me know if it is possible to do so or not. I want to leverage the inherited class in the second class and so this extension is from the InheritedClass instead of SuperClass.

Cheers,
Nitin


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:42 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I actually map a very similar, simple inheritance hierarchy in one of the Hibernate3 tutorials on my website. Fortunately, I us JPA as well. Here's the scenario Java classes:

Image

The mappings looked like this:

Ancestor without Mapping Strategy


Code:
package com.examscam.mappings;
import javax.persistence.*;
@Entity
public class Ancestor {
  private Long id;
  private String nationality;
  @Id
  @GeneratedValue
  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}
  public String getNationality() {return nationality;}
  public void setNationality(String nationality) {
    this.nationality = nationality;
  }
}



Parent Java Class



Code:
package com.examscam.mappings;
import javax.persistence.*;
@Entity
public class  Parentextends Ancestor{
  private String lastName;
  public String getLastName() {return lastName;}
  public void setLastName(String lastName) {
        this.lastName = lastName;
  }
}



Child Java Class



Code:
package com.examscam.mappings;
import javax.persistence.*;
@Entity
public class  Parentextends Ancestor{
  private String lastName;
  public String getLastName() {return lastName;}
  public void setLastName(String lastName) {
        this.lastName = lastName;
  }
}



Then I just added the following to the ancestor class:

Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Ancestor {    }



Running the code in a main method looked like this:

Code:
public static void main(String args[]) {
  Ancestor a = new Ancestor();
  a.setNationality("Korean");

  Parent p = new Parent();
  p.setNationality("Jewish");  p.setLastName("Steinberg");

  Child c = new Child();
  c.setNationality("Irish");
  c.setLastName("McKenzie");
  c.setFirstName("Cameron");

  Session session = HibernateUtil.beginTransaction();
  session.save(a); session.save(p); session.save(c);
  HibernateUtil.commitTransaction();
}



And when it was done, here was the tables and results:

Image


Image


And of course, the SQL:

Code:
Hibernate:
insert into Ancestor (nationality, DTYPE) values (?, 'Ancestor')
Hibernate:
insert into Ancestor (nationality, lastName, DTYPE) values (?, ?, 'Parent')
Hibernate:
insert into Ancestor (nationality, lastName, firstName, DTYPE) values (?, ?, ?, 'Child')

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:45 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
What u indent to do is perfectly possible. What exceptiona re u getting..

Try using InheritanceType.JOINED instead of single_table or look at the example at

http://www.oracle.com/technology/produc ... tance.html

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


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.