0 Replies Latest reply on Sep 22, 2007 8:14 AM by Thomas Trepper

    Mapping-Problem with Primary-Key containing three Foreign-Ke

    Thomas Trepper Newbie

      Hi all,

      I am sorry, but i cannot get it working. I have three tables with a PK beeing a single attribute. And then I have another table, with three attributes each referencing the PK of one table. And these three attributes are at the same time the PrimaryKey (PK)

      SQL looks like this:

      CREATE TABLE ListedSections (
       SectionID SMALLINT NOT NULL,
       SectionName CHARACTER VARYING(50) NOT NULL,
       SectionDescription CHARACTER VARYING(150),
       DateAdded TIMESTAMP DEFAULT current_timestamp,
       ActiveUntil TIMESTAMP,
       CONSTRAINT PK_ListedSections PRIMARY KEY (SectionID)
      );
      
      CREATE TABLE ListedRights (
       RightID SMALLINT NOT NULL,
       RightName CHARACTER VARYING(50) NOT NULL,
       RightDescription CHARACTER VARYING(150),
       DateAdded TIMESTAMP DEFAULT current_timestamp,
       ActiveUntil TIMESTAMP,
       CONSTRAINT PK_ListedRights PRIMARY KEY (RightID)
      );
      
      CREATE TABLE ListedRoles (
       RoleID SMALLINT NOT NULL,
       RoleName CHARACTER VARYING(40) NOT NULL,
       RoleDescription CHARACTER VARYING(150),
       DateAdded TIMESTAMP DEFAULT current_timestamp,
       ActiveUntil TIMESTAMP,
       CONSTRAINT PK_ListedRoles PRIMARY KEY (RoleID)
      );
      
      CREATE TABLE RoleHasRights (
       RightID SMALLINT NOT NULL,
       RoleID SMALLINT NOT NULL,
       SectionID SMALLINT NOT NULL,
       CONSTRAINT PK_RoleHasRights PRIMARY KEY (RightID, RoleID, SectionID)
      );
      
      ALTER TABLE RoleHasRights ADD CONSTRAINT ListedRights_RoleHasRights
       FOREIGN KEY (RightID) REFERENCES ListedRights (RightID);
      
      ALTER TABLE RoleHasRights ADD CONSTRAINT ListedRoles_RoleHasRights
       FOREIGN KEY (RoleID) REFERENCES ListedRoles (RoleID);
      
      ALTER TABLE RoleHasRights ADD CONSTRAINT ListedSections_RoleHasRights
       FOREIGN KEY (SectionID) REFERENCES ListedSections (SectionID);
      


      My Entities are looking like this:
      package myPackage;
      
      import java.io.Serializable;
      import java.sql.Timestamp;
      import java.util.Collection;
      import java.util.ArrayList;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      
      @Entity
      @Table(name="listedroles", schema="public", catalog="postgres")
      public class Role implements Serializable {
       private static final long serialVersionUID = 1L;
       private int roleid;
       private String roleName;
       private String roleDescription;
       private Timestamp dateadded;
       private Timestamp activeuntil;
       private Collection<RoleHasRights> roleHasRights = new ArrayList<RoleHasRights>();
      
       //--------------------------------------------------------------------------
       @Id
       public int getRoleid() {
       return this.roleid;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleid(int roleid) {
       this.roleid = roleid;
       }
      
       //--------------------------------------------------------------------------
       public String getRoleName() {
       return this.roleName;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleName(String roleName) {
       this.roleName = roleName;
       }
      
       //--------------------------------------------------------------------------
       public String getRoleDescription() {
       return this.roleDescription;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleDescription(String roleDescription) {
       this.roleDescription = roleDescription;
       }
      
       //--------------------------------------------------------------------------
       public Timestamp getDateadded() {
       return this.dateadded;
       }
      
       //--------------------------------------------------------------------------
       public void setDateadded(Timestamp dateadded) {
       this.dateadded = dateadded;
       }
      
       //--------------------------------------------------------------------------
       public Timestamp getActiveuntil() {
       return this.activeuntil;
       }
      
       //--------------------------------------------------------------------------
       public void setActiveuntil(Timestamp activeuntil) {
       this.activeuntil = activeuntil;
       }
      
       //--------------------------------------------------------------------------
       @OneToMany(mappedBy="roleHasRightsPK.roleid")
       @JoinColumn(name="roleHasRightsPK")
       public Collection<RoleHasRights> getRoleHasRights() {
       return this.roleHasRights;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleHasRights(Collection<RoleHasRights> roleHasRights) {
       this.roleHasRights = roleHasRights;
       }
      }
      


      Sections and Rights are nearly the same

      And RoleHasRights:
      package com.auctonova.ejb.admin.persitence;
      
      import java.io.Serializable;
      
      import javax.persistence.Table;
      import javax.persistence.Entity;
      import javax.persistence.Embeddable;
      import javax.persistence.EmbeddedId;
      import javax.persistence.ManyToOne;
      
      @Entity
      @Table(name="rolehasrights", schema="public", catalog="postgres")
      public class RoleHasRights implements Serializable {
       private static final long serialVersionUID = 1L;
       private RoleHasRightsPK roleHasRightsPK;
      
       //--------------------------------------------------------------------------
       @EmbeddedId
       @ManyToOne
       public RoleHasRightsPK getRoleHasRightsPK() {
       return roleHasRightsPK;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleHasRightsPK(RoleHasRightsPK roleHasRightsPK) {
       this.roleHasRightsPK = roleHasRightsPK;
       }
      
       //--------------------------------------------------------------------------
       public int getRoleid() {
       return this.roleHasRightsPK.roleid;
       }
      
       //--------------------------------------------------------------------------
       public void setRoleid(int roleid) {
       this.roleHasRightsPK.roleid = roleid;
       }
      
       //--------------------------------------------------------------------------
       public int getRightid() {
       return this.roleHasRightsPK.rightid;
       }
      
       //--------------------------------------------------------------------------
       public void setRightid(int rightid) {
       this.roleHasRightsPK.rightid = rightid;
       }
      
       //--------------------------------------------------------------------------
       public int getSectionid() {
       return this.roleHasRightsPK.sectionid;
       }
      
       //--------------------------------------------------------------------------
       public void setSectionid(int sectionid) {
       this.roleHasRightsPK.sectionid = sectionid;
       }
      
      
       //--------------------------------------------------------------------------
       @Embeddable
       public static class RoleHasRightsPK implements Serializable {
       private static final long serialVersionUID = 1L;
       private int roleid;
       private int rightid;
       private int sectionid;
      
       //----------------------------------------------------------------------
       public RoleHasRightsPK() {
       super();
       }
      
       //----------------------------------------------------------------------
       public int getRoleid() {
       return roleid;
       }
      
       //----------------------------------------------------------------------
       public void setRoleid(int roleid) {
       this.roleid = roleid;
       }
      
       //----------------------------------------------------------------------
       public int getRightid() {
       return rightid;
       }
      
       //----------------------------------------------------------------------
       public void setRightid(int rightid) {
       this.rightid = rightid;
       }
      
       //----------------------------------------------------------------------
       public int getSectionid() {
       return sectionid;
       }
      
       //----------------------------------------------------------------------
       public void setSectionid(int sectionid) {
       this.sectionid = sectionid;
       }
      
       //----------------------------------------------------------------------
       @Override
       public boolean equals(Object o) {
       if (o == this) {
       return true;
       }
       if ( ! (o instanceof RoleHasRightsPK)) {
       return false;
       }
       RoleHasRightsPK other = (RoleHasRightsPK) o;
       return (this.roleid == other.roleid)
       && (this.rightid == other.rightid)
       && (this.sectionid == other.sectionid);
       }
      
       //----------------------------------------------------------------------
       @Override
       public int hashCode() {
       return ((int) (this.roleid ^ (this.roleid >>> 32)))
       ^ ((int) (this.rightid ^ (this.rightid >>> 32)))
       ^ this.sectionid;
       }
       }
      }
      


      Can anybody help my getting this working? Thank you very much and kind regards,

      Thomas