-->
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: Persisting properties of parent class with annotation
PostPosted: Sun Jun 29, 2008 11:44 am 
Newbie

Joined: Fri Jan 04, 2008 9:07 am
Posts: 2
Hello,

I have a class Y, that extends class X. With hibernate annotations, how do I tell to persist some properties of the parent class?
Thanks before.

Rudi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 2:18 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
That depends. If your superclass is used polymorphically, then you use
Code:
@Entity
.
If not, then you use
Code:
@MappedSuperClass
.

Either way, just annotate the properties of your superclass as usual.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 10:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Of course, if the properties of the parent are passed down to the child class, they should be automatically be persisted if the inheritance mapping is done correctly.

Here's a little tutorial on how to map inherited classes using Hibernate and JPA annotations. It should clear up any doubt:

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

Regardless, the key is how the classes are mapped. Here's a simple inheritance mapping example:


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


When this code runs, you get all of the inherited properties persisted, as you can see from the database snapshot.

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


Image

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  
 
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.