-->
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: Mapping inheritance JPA
PostPosted: Thu Feb 14, 2008 12:41 pm 
Newbie

Joined: Thu Feb 14, 2008 11:47 am
Posts: 2
Surfing the net to understand how to map with jpa a concrete class
extending an abstract one and implementing an interface we saw on this
link
[url]
http://www.hibernate.org/hib_docs/annot ... ntity.html
[/url]

that it is not possible , but we achieved to do that in a tricky way shown below :

We would like to know if it could be a solution , and if there are any other annotations with jpa which simplifies much our development

greetings...

Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public interface Animale
{
   @Id
   public Integer getId();
   
   public void setId(Integer id);
   
   public void cammina();
}


Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Bestiola {

   @Id@GeneratedValue(strategy=GenerationType.TABLE)
   private Integer id;

   public Integer getId() {
      return id;
   }
   
   public void setId(Integer id) {
      this.id = id;
   }
}


Code:
@Entity
public class Cane extends Bestiola implements Animale{   
   
   @OneToMany
   private Collection<Animale> l = null;

   
   public Collection<Animale> getL() {
      return l;
   }

   public void setL(Collection<Animale> l) {
      this.l = l;
   }

   public void cammina() {
   }

   public Integer getId() {
      return super.getId();
   }

   public void setId(Integer id) {
      super.setId(id);
   }
   
}


Code:
@Entity
public class Gatto extends Bestiola implements Animale{


   public void cammina() {
   }

   public Integer getId() {
      return super.getId();
   }

   public void setId(Integer id) {
      super.setId(id);
   }
}


Code:
public class TestCane extends TestCase{
   private EntityManagerFactory emf = null;
   private EntityManager em = null;
   private EntityTransaction et = null;
   
   public void setUp(){
      emf = Persistence.createEntityManagerFactory("Test");
      em = emf.createEntityManager();
   }
   
   public void testA(){
      et = em.getTransaction();
      et.begin();
      
         Cane c = new Cane();
         Cane c2 = new Cane();
         Gatto g = new Gatto();
         
         Collection coll = new ArrayList<Animale>();
         coll.add(c2);
         coll.add(g);
         c.setL(coll);
         
         em.persist(g);
         em.persist(c2);
         em.persist(c);
         
      et.commit();
      
   }
   
   public void tearDown(){
      em.close();
      emf.close();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 8:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
putting annoations on interfaces is not really supported. If it works fine, but there is a good change we will break that in the future when the feature will be properly implemented.

_________________
Emmanuel


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.