-->
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.  [ 5 posts ] 
Author Message
 Post subject: ManyToMany with polymorphism, Deadly Mix?
PostPosted: Wed Sep 20, 2006 10:20 pm 
Newbie

Joined: Wed Sep 20, 2006 10:07 pm
Posts: 3
Hibernate version: 3.2cr4 Hibernate Annotation 3.2.0 cr2

I am designing a model which requires a many-to-many mapping from class:Tag to a class hierarchy
Code:
     TagPojo
         *
         |         |-------- EmailPojo
         *         V
ObjectPojo <-- MessagePojo <--- DocumentPojo
       ^
       |____ ImagePojo
       |
       |____ MusicPojo


I used
Code:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS
on all the classes in ObjectPojo hierarchy, it works fine. But when I try to create many-to-many mapping between TagPojo and ObjectPojo, it failed because ObjectPojo is just an abstract class with no data/instance.

in theory, I need to facilitate linking a tag with any instance from either EmailPojo, DocumentPojo, ImagePojo or MusicPojo class.

Here's the many-to-many mapping I am using now
Code:
   @ManyToMany(targetEntity = ObjectPojo.class, fetch = FetchType.EAGER, cascade = {
         CascadeType.PERSIST, CascadeType.MERGE })
   @JoinTable(name = "TAG_OBJECT_MAPPING", joinColumns = { @JoinColumn(name = "TAG_ID") }, inverseJoinColumns = { @JoinColumn(name = "OBJECT_ID") })
class TagPojo { ... }

Any thoughts on how to handle this model? Thanks


Top
 Profile  
 
 Post subject: Encountered the same problem
PostPosted: Tue Aug 28, 2007 7:35 am 
Newbie

Joined: Tue Aug 28, 2007 7:27 am
Posts: 7
Is there any workaround ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 12:34 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

Quote:
ObjectPojo is just an abstract class with no data/instance.


why don't you map your abstract class as Entity?
If you want to use Polymorphismus you have to map
all classes as Entity.

In this class you can use SingleTable as Inheritance Strategy, to avoid any additional tables.

@MappedSuperclass is only possible if no Polymorphismus is used.

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 6:45 am 
Newbie

Joined: Tue Aug 28, 2007 7:27 am
Posts: 7
We tried out the SingleTable inheritance with a ManyToMany

Code:
@Entity
@Table(name="SHB_PRINCIPAL")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE",discriminatorType=DiscriminatorType.INTEGER)
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Principal implements Serializable {
....


Code:
@Entity
@DiscriminatorValue(PrincipalType.USER_NR)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ApplicationUser extends Principal {

   private static final long serialVersionUID = 1L;
         
   @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},
         targetEntity=Role.class)
   @JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
      joinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"),
      inverseJoinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"))
   private Set<Role> roles;
   
   @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},
         targetEntity=ApplicationGroup.class)
   @JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
      joinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"),
      inverseJoinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"))      
   private Set<ApplicationGroup> groups;



Code:
@Entity
@DiscriminatorValue(PrincipalType.GROUP_NR)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ApplicationGroup extends Principal {

   private static final long serialVersionUID = 1L;   

   @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
   @JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
      joinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"),
      inverseJoinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"))      
   private Set<ApplicationUser> users;
   
   public ApplicationGroup() {
      super();
      this.type = PrincipalType.GROUP;
   }

   public Set<ApplicationUser> getUsers() {
      return users;
   }
   public void setUsers(Set<ApplicationUser> users) {
      this.users = users;
   }
}


Code:
@Entity
@Table(name="SHB_PRINCIPAL_TO_PRINCIPAL")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class PrincipalPrincipal implements Serializable {
   
   private static final long serialVersionUID = 1L;
   
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="ID")
   private Integer id;

   @ManyToOne
   @JoinColumn(name="FROM_PRINCIPAL_ID")
   private Principal fromPrincipal;
   
   @Required
   @ManyToOne
   @JoinColumn(name="TO_PRINCIPAL_ID")
   private Principal toPrincipal;
   
   public PrincipalPrincipal() {
      super();
   }

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

   public Principal getFromPrincipal() {
      return fromPrincipal;
   }

   public void setFromPrincipal(Principal fromPrincipal) {
      this.fromPrincipal = fromPrincipal;
   }

   public Principal getToPrincipal() {
      return toPrincipal;
   }

   public void setToPrincipal(Principal toPrincipal) {
      this.toPrincipal = toPrincipal;
   }
}




But in the groups and the roles of the user, hibernate returns all principals, not only the users or groups. In the query it's clear that the descriminator is NOT used.


Top
 Profile  
 
 Post subject: There is a unit test available that simulates the problem
PostPosted: Wed Jan 09, 2008 11:20 am 
Newbie

Joined: Tue Aug 28, 2007 7:27 am
Posts: 7
http://opensource.atlassian.com/project ... e/HHH-2883


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