-->
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: many to many relationship to an abstract class?
PostPosted: Wed Mar 19, 2008 12:59 pm 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
Code:
public abstract class Building {
   @Id
   Long resourceId;
   
   @ManyToMany(cascade = { CascadeType.ALL })
   private List<Project> projectList;
}

@Entity
public class Office extends Building {}

@Entity
public class House extends Building {}

@Entity
public class Project {
   
   @Id
   Long id;   
   
   @ManyToMany(cascade = { CascadeType.ALL })
   private List<Building> buildingList;  // here is my problem!!!!!

}


My question is, how do I implement a bidirectional many-to-may relationship between Project and Building? I want to avoid duplicating the relationship from Project to House and from Project to Building. Therefore I want the many to many relation from Project to Building. And I want to create a Project, add an Office to it and save it (entityManager.persist(project)).

I am a dead man by now tried a lot of options, @CollectionOfElements on buildingList ,InheritanceType.SINGLE_TABLE on Building. It all did not work.

Anyone knows which annotations I have to add in order to make this work?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 20, 2008 7:45 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
put a @Entity and @Inheritance (single table) on Building, write a HQL "... from Building" that proves that that polymorfism works.
Next follow the intructions in the manual on how to map a ManyToMany.

PS: I personally dislike @ManyToMany and always use a relation object (say ProjectBuildingAssigment) with OneToMany's

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 9:08 am 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
It worked, thanks!

Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class ProjectObject extends Resource


I also had to add InheritanceType.JOINED because there is a bug in Hibernate with PostGres and this solves it.


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.