3 Replies Latest reply on Sep 8, 2005 1:04 PM by dornus

    How do I use a primary key that is a foreign key to another

      I have 2 tables.

      Table 1
      ---------
      int myId (Primary Key)
      etc...

      Table 2
      ---------
      int myForeignId (Primary Key, and is a foreign key of Table 1's myId)
      etc...

      How do I set up annotations for table2?
      I have tried tagging myForeignId with @Id, @JoinColumn, separately and together.

      What is the correct way to do this?

      Thanks

        • 1. Re: How do I use a primary key that is a foreign key to anot

          Is anyone able to answer this?

          I have the following which is not working. I need to know the correct way of doing this.

          public class TableOne{
           private int myId;
           // more variables, etc...
          
           @Id(generate = GeneratorType.AUTO)
           public int getMyId() {
           return myId;
           }
          
           public void setMyId( int myId ) {
           this.myId = myId;
           }
          
           // more getters and setters for misc variables etc...
          }
          
          public class TableTwo{
           private TableOne myId;
           // more variables, etc...
          
           @Id(generate = GeneratorType.AUTO) //DOESN'T WORK
           @ManyToOne //DOESN'T WORK
           @JoinColumn(name = "myForeignId") //DOESN'T WORK
           public TableOne getMyId() {
           return myId;
           }
          
           public void setMyId( TableOne myId ) {
           this.myId = myId;
           }
          
           // more getters and setters for misc variables etc...
          }
          
          


          • 2. Re: How do I use a primary key that is a foreign key to anot
            epbernard

             

            @Id
            String myOtherEntityId;
            
            @ManyToOne
            @JoinColumn(insertable=false, updatable=false, name="myOtherEntityId")
            MyOtherEntity myOtherEntity;


            • 3. Re: How do I use a primary key that is a foreign key to anot

               

              "epbernard" wrote:
              @Id
              String myOtherEntityId;
              
              @ManyToOne
              @JoinColumn(insertable=false, updatable=false, name="myOtherEntityId")
              MyOtherEntity myOtherEntity;


              Excellent! Thank you for the response. I modified the code to the following and it works.

              
              public class TableTwo{
               private int myForeignId;
               private TableOne myId;
               // more variables, etc...
              
               @Id
               public int getMyForeignId() {
               return myForeignId;
               }
               public void setMyForeignId( int myForeignId ) {
               this.myForeignId = myForeignId;
               }
              
               @ManyToOne
               @JoinColumn(insertable=false, updatable=false, name="myForeignId")
               public TableOne getMyId() {
               return myId;
               }
              
               public void setMyId( TableOne myId ) {
               this.myId = myId;
               }
              
               // more getters and setters for misc variables etc...
              }