-->
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.  [ 4 posts ] 
Author Message
 Post subject: Foreign keys as composite primary key
PostPosted: Tue Jul 06, 2010 5:13 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
I have four table like USER, ROLE, PERMISSION and USERROLEPERMISSION.
I have a composite Primary Key in USERROLEPERMISSION and all the fields in the composite primary key are also foreign keys.

The primary keys for these table are
1. USER : UserID
2. ROLE : RoleID
3. PERMISSION : PermissionID
4. USERROLEPERMISSION : (UserID,RoleID,PermissionID) and these fields are also foreign key to this table towards the other tables.

I have declared the primary key and foreign key as follows.

Code:
  public class UserRolePermissionDetailsDO {
   
   @Id
   @JoinColumn(name = "UserID")
        @ManyToOne(optional = true)
        @ForeignKey(name = "UserID")
   private UserDO userDO;
   
   @Id
   @JoinColumn(name = "RoleID")
        @ManyToOne(optional = true)
        @ForeignKey(name = "RoleID")
        private RoleDO roleDO;
   
   @Id
   @JoinColumn(name = "PermissionsID")
        @ManyToOne(optional = true)
        @ForeignKey(name = "PermissionsID")
   private PermissionDO permissionDO;   


In USERDO I have the corresponding entry as below
Code:
       @OneToMany(cascade = {CascadeType.REMOVE}, mappedBy="userDO", fetch = FetchType.LAZY)   
   private Set<UserRolePermissionDetailsDO> userRolePermissionDetailsDO;


In ROLEDO I have the corresponding entry as below
Code:
       @OneToMany(cascade = {CascadeType.REMOVE}, mappedBy="roleDO", fetch = FetchType.LAZY)   
   private Set<UserRolePermissionDetailsDO> userRolePermissionDetailsDO;


In PERMISSIONDO I have the corresponding entry as below
Code:
       @OneToMany(cascade = {CascadeType.REMOVE}, mappedBy="permissionDO", fetch = FetchType.LAZY)   
   private Set<UserRolePermissionDetailsDO> userRolePermissionDetailsDO;


This throws an error

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.org.do.UserRolePermissionDetailsDO.roleDO in com.org.do.RoleDO.userRolePermissionDetailsDO

Any idea please.


Top
 Profile  
 
 Post subject: Re: Foreign keys as composite primary key
PostPosted: Thu Jul 08, 2010 4:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I think that, even though you asked this question on the wrong forum (you should use the Hibernate Users forum), you identified a bug. I created a Jira issue HHH-5359 for it.

One thing regarding your mapping, the associations in UserRolePermissionDetailsDO cannot be optional when they are ids, but that's not the problem in this case. If you really want to have optional associations in UserRolePermissionDetailsDO you should introduce a suggorate key (which I would recommend anyways).

--Hardy


Top
 Profile  
 
 Post subject: Re: Foreign keys as composite primary key
PostPosted: Mon Jul 19, 2010 12:21 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Hi All,

I have solved the issue with following changes in UserRolePermissionDetailsDO, the code is as shown below

Code:
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name = "USERPERMISSIONSDETAILS")

public class UserPermissionDetailsDO extends BaseDO {
   
   @Id
   private UserRolePermissionPK primaryKey = new CloudRolePermissionPK();
   
   @SuppressWarnings("unused")
   @JoinColumn(name = "UserID", nullable=false, insertable=false, updatable=false)
        @ManyToOne(optional = true)
        @ForeignKey(name = "UserID")
   private UserDO userDO;
   
   @SuppressWarnings("unused")
   @JoinColumn(name = "RoleID", nullable=false, insertable=false, updatable=false)
        @ManyToOne(optional = true)
        @ForeignKey(name = "RoleID")
        private RoleDO roleDO;
   
   @SuppressWarnings("unused")
   @JoinColumn(name = "PermissionsID", nullable=false, insertable=false, updatable=false)
        @ManyToOne(optional = true)
        @ForeignKey(name = "PermissionsID")
   private PermissionDO permissionDO;   
   
   /**
    * @param userDO the userDO to set
    */
   public void setUserDO(UserDO userDO) {
      this.userDO= userrDO;
      this.primaryKey.setUserDO(userDO);
   }

   /**
    * @return the cloudDO
    */
   public CloudDO getCloudDO() {
      return primaryKey.getCloudDO();
   }

   /**
    * @param roleDO the roleDO to set
    */
   public void setRoleDO(RoleDO roleDO) {
      this.roleDO = roleDO;
      this.primaryKey.setRoleDO(roleDO);
   }

   /**
    * @return the roleDO
    */
   public RoleDO getRoleDO() {
      return primaryKey.getRoleDO();
   }

   /**
    * @param permissionDO the permissionDO to set
    */
   public void setPermissionDO(PermissionDO permissionDO) {
      this.permissionDO = permissionDO;
      this.primaryKey.setPermission(permissionDO);
   }

   /**
    * @return the permissionDO
    */
   public PermissionDO getPermissionDO() {
      return primaryKey.getPermission();
   }
      

   @SuppressWarnings("serial")
   @Embeddable
   private static class UserdRolePermissionPK implements Serializable
     {

      @JoinColumn(name = "CloudID")
       @ManyToOne(optional = false)
       private UserDO userDO;

       @JoinColumn(name = "RoleID")
       @ManyToOne(optional = false)
       private RoleDO roleDO;
      
       @JoinColumn(name = "PermissionsID")
       @ManyToOne(optional = false)
       private PermissionDO permission;

      /**
       * @return the userDO
       */
      public UserDO getUserDO() {
         return userDO;
      }

      /**
       * @param userDO the userDO to set
       */
      public void setUserDO(userDO userDO) {
         this.userDO = userDO;
      }

      /**
       * @return the roleDO
       */
      public RoleDO getRoleDO() {
         return roleDO;
      }

      /**
       * @param roleDO the roleDO to set
       */
      public void setRoleDO(RoleDO roleDO) {
         this.roleDO = roleDO;
      }

      /**
       * @return the permission
       */
      public PermissionDO getPermission() {
         return permission;
      }

      /**
       * @param permission the permission to set
       */
      public void setPermission(PermissionDO permission) {
         this.permission = permission;
      }      
     }}



Top
 Profile  
 
 Post subject: Re: Foreign keys as composite primary key
PostPosted: Fri Aug 27, 2010 10:14 pm 
Newbie

Joined: Wed Aug 25, 2010 1:24 pm
Posts: 1
Thanks man !! Hibernate can be such a pain sometimes !


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