-->
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.  [ 1 post ] 
Author Message
 Post subject: model ManyToMany as 2 OneToMany validation sought.
PostPosted: Mon May 07, 2007 9:52 am 
Newbie

Joined: Mon Nov 03, 2003 1:07 am
Posts: 14
I have the a Role and a Permission object that I modeled with a ManyToMany relationship (role.getPermissions() and permission.getRoles()) The problem I encountered with this bi-directional ManyToMany was that I could only have changes made to one end be persistent, where I wanted to be able to manage this relationship from both ends (Role and Permission)

I decided to go with 2 OneToMany's to solve this problem (does it?)

I created an assocation object "RolePermission" The Role has a OneToMany to RolePermission, and the Permission has a OneToMany to RolePermission.

The Role and the Permission are the owners of the relationship, I did this by not using the "mappedBy" as described in the Soldier/Troop example (in the hiberate annotation reference documentation)

I would really really appeciate it if someone could let me know if this is the way to go on this. I have many other associative relationships of this type and it would be good to know if this is a mistake before I change 100's of lines of code.

Thanks.
-Phillip


Hibernate version:
3.2.1.ga

Mapping documents:
Code:
package org.authsum.om;

import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "authsum_role_permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class RolePermission {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "object_id")
   public Long getObjectId() {
      return (objectId);
   }

   /**
    * The setter method for this object identifier.
    */
   public void setObjectId(Long objectId) {
      this.objectId = objectId;
   }

   private Long objectId;

   private Role role = null;

   private Permission permission = null;

   @ManyToOne
   @JoinColumn(name = "permission_fk", insertable = false, updatable = false)
   public Permission getPermission() {
      return permission;
   }

   public void setPermission(Permission permission) {
      this.permission = permission;
   }

   @ManyToOne
   @JoinColumn(name = "role_fk", insertable = false, updatable = false)
   public Role getRole() {
      return role;
   }

   public void setRole(Role role) {
      this.role = role;
   }

}




package org.authsum.om;

import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "authsum_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "object_id")
   public Long getObjectId() {
      return (objectId);
   }

   /**
    * The setter method for this object identifier.
    */
   public void setObjectId(Long objectId) {
      this.objectId = objectId;
   }

   private Long objectId;


   @OneToMany(cascade = {CascadeType.ALL})     
   @JoinColumn(name="role_fk")
   public Set<RolePermission> getRolePermissions() {
      return rolePermissions;
   }

   public void setRolePermissions(Set<RolePermission> rolePermissions) {
      this.rolePermissions = rolePermissions;
   }
   private Set<RolePermission> rolePermissions = new HashSet<RolePermission>();   
}





package org.authsum.om;

import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "authsum_permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "object_id")
   public Long getObjectId() {
      return (objectId);
   }

   /**
    * The setter method for this object identifier.
    */
   public void setObjectId(Long objectId) {
      this.objectId = objectId;
   }

   private Long objectId;


   @OneToMany(cascade = {CascadeType.ALL})
   @JoinColumn(name="permission_fk")
   public Set<RolePermission> getRolePermissions() {
      return rolePermissions;
   }

   public void setRolePermissions(Set<RolePermission> rolePermissions) {
      this.rolePermissions = rolePermissions;
   }


   private Set<RolePermission> rolePermissions = new HashSet<RolePermission>();
   

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.