3 Replies Latest reply on Jan 30, 2006 3:35 PM by bill.burke

    mappedBy to a PK property

    cesarizurieta

      Hi, I'm tring to do the following:

      @Entity
      public class One {
       private OnePK id;
      
       @Id
       public OnePK getId() { return id; }
       public setId(OnePK id) { this.id = id; }
      }
      @Embeddable
      public class OnePK {
       private int a;
       private Two two;
      
       public A getA() { return a; }
       public setA(A) { this.a = a; }
      
       @ManyToOne
       public Two getTwo() { return two; }
       public setTwo(Two two) { this.two = two; }
      }
      @Entity
      public class Two {
       private int id;
       private Set<One> ones;
      
       @Id
       public int getId() { return id; }
       public setId(int id) { this.id = id; }
      
       @OneToMany(mappedBy="two")
       public Set<One> getOnes() { return ones; }
       public setOnes(Set<One> ones) { this.ones = ones; }
      }


      But the mappedBy is not working. Am I doing something wrong? Thanks.

        • 1. Re: mappedBy to a PK property
          bill.burke

          what you are trying to do is not legal in the specification. Only basic types are allowed within Embeddables

          • 2. Re: mappedBy to a PK property
            cesarizurieta

            What if I use @IdClass like this (removing the @ManyToOne from the PK class):

            @Entity
            @IdClass(OnePK.class)
            public class One {
             private int a;
             private Two two;
            
             @Id
             public A getA() { return a; }
             public setA(A) { this.a = a; }
            
             @Id
             @ManyToOne
             public Two getTwo() { return two; }
             public setTwo(Two two) { this.two = two; }
            }


            It doesn't work either. Is there any other way I can map a collection to a composite PK field?

            • 3. Re: mappedBy to a PK property
              bill.burke

              I don't remember if @PrimaryKeyJoinColumn exists in RC3, but you can try this out on the RC4 release that we are putting out today.

              @Entity
              public class One {
               private OnePK one;
               private Two two;
              
               @EmbeddedId
               public OnePk getPK() { return a; }
               public setPK(OnePK) { this.one= one; }
              
               @ManyToOne
               @PrimaryKeyJoinColumn(name="a_column_name")
               public Two getTwo() { return two; }
               public setTwo(Two two) { this.two = two; }
              }
              
              @Embeddable
              OnePK (
               private int a;
               private int b;
              
              // get/set methods
               @Column("a_column_name")
               public getA() { return a; }
              }
              
              


              either use @PrimaryKeyJoinColumn(name) or (referencedName) I don't remember which is the right one off the top of my head.