-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate mappy for polymorphism model design
PostPosted: Wed Jul 02, 2008 5:16 pm 
Newbie

Joined: Wed Jul 02, 2008 5:02 pm
Posts: 1
We have a legacy application that currently uses object-oriented database and we hope that we can switch to ORDB. The model design is highly object centric with layers of inheritance. The follow is one simplified example of the data model.

public abstract class A<I extends B> {
… // A’s properties
List<I> exampleList = new ArrayList<I>();

}

public class AA extends A< BB extends B> {
… // AA’s own properties in addition to A’s properties
// including exampleList
BB myBB;
}

public class B { // this is even not a abstract class
… // B’s properties

}

public class BB extends B {
...; // BB’s own properties in addition to B’s properties
}

I am new to Hibernate. Could anybody give me suggestion on how to map these four classes nicely? Table per concrete class with implicit polymorphism will fit my needs well if I can use it but it doesn’t support one-to-many mapping if I understand correctly ...

Thank you in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 1:17 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I actually think single table would fit your needs best, especially if you have many to many relationships.

On my website (and in my book) I map the following inheritance hierarchy using Hibernate:

Image

It's actually pretty simple:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=16mappinginheritancewithjpa

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;
  }
}


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;
  }
}
Code:
package com.examscam.mappings;
import javax.persistence.*;
@Entity
public class Child extends Parent {
  private String firstName;
  public String getFirstName() {return firstName;}
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}


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();
}


When you're done, you get a very simple and manageable talbe with your data in it. Mappings and associations can be added, and they fit into the single talbe nicely:

Image

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=16mappinginheritancewithjpa

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