How to make Composite Primary Key Using Two Foreign key?











up vote
0
down vote

favorite












I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.



CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));


table2



CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));


table3



CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);


How to convert this table into an Hibenate Entity.



 @Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}

@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}

@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;

//getter and Setter
}


@Embeddable
Class table3PK{

@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;

@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;

public table3PK(){

}

public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}


}


}










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.



    CREATE TABLE table1
    (table1id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (table1id));


    table2



    CREATE TABLE table2
    (table2id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (table2id));


    table3



    CREATE TABLE table3 (
    table1id INT NOT NULL,
    table2id INT NOT NULL,
    PRIMARY KEY (table1id, table2id),
    FOREIGN KEY (table1id) REFERENCES table1 (table1id),
    FOREIGN KEY (table2id) REFERENCES table2 (table2id)
    );


    How to convert this table into an Hibenate Entity.



     @Entity
    @Table(name="table3")
    class Table1 {
    @Id
    long table1id;
    //getter and setter
    }

    @Entity
    @Table(name="table3")
    class Table2 {
    @Id
    long table2id;
    //getter and Setter
    }

    @Entity
    @Table(name="table3")
    class Table3 {
    @EmbeddedId
    private table3PK table3PKId;

    //getter and Setter
    }


    @Embeddable
    Class table3PK{

    @ManyToOne
    @JoinColumn(name="table1Id" ,referencedColumnName="table1id")
    Table1 table1;

    @ManyToOne
    @JoinColumn(name="table2Id" ,referencedColumnName="table2id")
    Table2 table2;

    public table3PK(){

    }

    public table3PK(Table1 table1 ,Table2 table2){
    this.table1;
    this.table2;
    }


    }


    }










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.



      CREATE TABLE table1
      (table1id INT NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (table1id));


      table2



      CREATE TABLE table2
      (table2id INT NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (table2id));


      table3



      CREATE TABLE table3 (
      table1id INT NOT NULL,
      table2id INT NOT NULL,
      PRIMARY KEY (table1id, table2id),
      FOREIGN KEY (table1id) REFERENCES table1 (table1id),
      FOREIGN KEY (table2id) REFERENCES table2 (table2id)
      );


      How to convert this table into an Hibenate Entity.



       @Entity
      @Table(name="table3")
      class Table1 {
      @Id
      long table1id;
      //getter and setter
      }

      @Entity
      @Table(name="table3")
      class Table2 {
      @Id
      long table2id;
      //getter and Setter
      }

      @Entity
      @Table(name="table3")
      class Table3 {
      @EmbeddedId
      private table3PK table3PKId;

      //getter and Setter
      }


      @Embeddable
      Class table3PK{

      @ManyToOne
      @JoinColumn(name="table1Id" ,referencedColumnName="table1id")
      Table1 table1;

      @ManyToOne
      @JoinColumn(name="table2Id" ,referencedColumnName="table2id")
      Table2 table2;

      public table3PK(){

      }

      public table3PK(Table1 table1 ,Table2 table2){
      this.table1;
      this.table2;
      }


      }


      }










      share|improve this question













      I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.



      CREATE TABLE table1
      (table1id INT NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (table1id));


      table2



      CREATE TABLE table2
      (table2id INT NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (table2id));


      table3



      CREATE TABLE table3 (
      table1id INT NOT NULL,
      table2id INT NOT NULL,
      PRIMARY KEY (table1id, table2id),
      FOREIGN KEY (table1id) REFERENCES table1 (table1id),
      FOREIGN KEY (table2id) REFERENCES table2 (table2id)
      );


      How to convert this table into an Hibenate Entity.



       @Entity
      @Table(name="table3")
      class Table1 {
      @Id
      long table1id;
      //getter and setter
      }

      @Entity
      @Table(name="table3")
      class Table2 {
      @Id
      long table2id;
      //getter and Setter
      }

      @Entity
      @Table(name="table3")
      class Table3 {
      @EmbeddedId
      private table3PK table3PKId;

      //getter and Setter
      }


      @Embeddable
      Class table3PK{

      @ManyToOne
      @JoinColumn(name="table1Id" ,referencedColumnName="table1id")
      Table1 table1;

      @ManyToOne
      @JoinColumn(name="table2Id" ,referencedColumnName="table2id")
      Table2 table2;

      public table3PK(){

      }

      public table3PK(Table1 table1 ,Table2 table2){
      this.table1;
      this.table2;
      }


      }


      }







      hibernate jpa spring-data-jpa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 13:09









      Prabhat Yadav

      1762417




      1762417
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:



          @Entity
          public class Table1 {
          @Id
          long table1id;
          // ...
          }

          @Entity
          public class Table2 {
          @Id
          long table2id;
          // ...
          }

          @Embeddable
          public class Table3PK {
          long table1PK; // corresponds to PK type of Table1
          long table2PK; // corresponds to PK type of Table2
          }

          @Entity
          public class Table3 {
          @EmbeddedId
          private Table3PK id;

          @MapsId("table1PK") // maps table1PK attribute of embedded id
          @ManyToOne
          Table1 table1;

          @MapsId("table2PK") // maps table2PK attribute of embedded id
          @ManyToOne
          Table2 table2;

          // ...
          }


          Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375369%2fhow-to-make-composite-primary-key-using-two-foreign-key%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:



            @Entity
            public class Table1 {
            @Id
            long table1id;
            // ...
            }

            @Entity
            public class Table2 {
            @Id
            long table2id;
            // ...
            }

            @Embeddable
            public class Table3PK {
            long table1PK; // corresponds to PK type of Table1
            long table2PK; // corresponds to PK type of Table2
            }

            @Entity
            public class Table3 {
            @EmbeddedId
            private Table3PK id;

            @MapsId("table1PK") // maps table1PK attribute of embedded id
            @ManyToOne
            Table1 table1;

            @MapsId("table2PK") // maps table2PK attribute of embedded id
            @ManyToOne
            Table2 table2;

            // ...
            }


            Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.






            share|improve this answer

























              up vote
              0
              down vote













              It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:



              @Entity
              public class Table1 {
              @Id
              long table1id;
              // ...
              }

              @Entity
              public class Table2 {
              @Id
              long table2id;
              // ...
              }

              @Embeddable
              public class Table3PK {
              long table1PK; // corresponds to PK type of Table1
              long table2PK; // corresponds to PK type of Table2
              }

              @Entity
              public class Table3 {
              @EmbeddedId
              private Table3PK id;

              @MapsId("table1PK") // maps table1PK attribute of embedded id
              @ManyToOne
              Table1 table1;

              @MapsId("table2PK") // maps table2PK attribute of embedded id
              @ManyToOne
              Table2 table2;

              // ...
              }


              Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:



                @Entity
                public class Table1 {
                @Id
                long table1id;
                // ...
                }

                @Entity
                public class Table2 {
                @Id
                long table2id;
                // ...
                }

                @Embeddable
                public class Table3PK {
                long table1PK; // corresponds to PK type of Table1
                long table2PK; // corresponds to PK type of Table2
                }

                @Entity
                public class Table3 {
                @EmbeddedId
                private Table3PK id;

                @MapsId("table1PK") // maps table1PK attribute of embedded id
                @ManyToOne
                Table1 table1;

                @MapsId("table2PK") // maps table2PK attribute of embedded id
                @ManyToOne
                Table2 table2;

                // ...
                }


                Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.






                share|improve this answer












                It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:



                @Entity
                public class Table1 {
                @Id
                long table1id;
                // ...
                }

                @Entity
                public class Table2 {
                @Id
                long table2id;
                // ...
                }

                @Embeddable
                public class Table3PK {
                long table1PK; // corresponds to PK type of Table1
                long table2PK; // corresponds to PK type of Table2
                }

                @Entity
                public class Table3 {
                @EmbeddedId
                private Table3PK id;

                @MapsId("table1PK") // maps table1PK attribute of embedded id
                @ManyToOne
                Table1 table1;

                @MapsId("table2PK") // maps table2PK attribute of embedded id
                @ManyToOne
                Table2 table2;

                // ...
                }


                Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 3:31









                Brian Vosburgh

                1,9831911




                1,9831911






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375369%2fhow-to-make-composite-primary-key-using-two-foreign-key%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy