org.hibernate.MappingException: Foreign key XXX must have same number of columns as the referenced primary...












1















Having a following SQL table:



create table users_posts_ratings_map (
postId integer not null references posts (id),
userId integer not null references users (id),
ratingId integer not null references ratings (id),
primary key (postId, userId)
);


and Following JPA-Annotated POJOs:



RatingId.java:



@Embeddable
public class RatingId implements Serializable {
@ManyToOne
@JoinColumn(name = "userId")
private User user;

@ManyToOne
@JoinColumn(name = "postId")
private Post post;

// getters and setters
}


UserPostRating.java:



@Entity(name = "users_posts_ratings_map")
public class UserPostRating {
@EmbeddedId
private RatingId userPost;

@OneToOne
@JoinColumn(name = "ratingId")
private Rating rating;

// getters and setters
}


Post.java



@Entity(name = "posts")
public class Post {
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

// irrelevant fields

@ManyToMany
@JoinTable(
name = "users_posts_ratings_map",
joinColumns = { @JoinColumn(name = "ratingId") },
inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
)
private Set<UserPostRating> ratings = new HashSet<>();

// getters and setters
}


I am getting



org.hibernate.MappingException: Foreign key (FKB278E73083D94769:users_posts_ratings_map [postId,userId])) must have same number of columns as the referenced primary key (users_posts_ratings_map [ratingId,postId,userId])


on servlet container initialization stage.



What does it mean (What are Foreign Keys in this mappings? What are Primary Keys? Which annotations are marking what?) and how it could be fixed?










share|improve this question



























    1















    Having a following SQL table:



    create table users_posts_ratings_map (
    postId integer not null references posts (id),
    userId integer not null references users (id),
    ratingId integer not null references ratings (id),
    primary key (postId, userId)
    );


    and Following JPA-Annotated POJOs:



    RatingId.java:



    @Embeddable
    public class RatingId implements Serializable {
    @ManyToOne
    @JoinColumn(name = "userId")
    private User user;

    @ManyToOne
    @JoinColumn(name = "postId")
    private Post post;

    // getters and setters
    }


    UserPostRating.java:



    @Entity(name = "users_posts_ratings_map")
    public class UserPostRating {
    @EmbeddedId
    private RatingId userPost;

    @OneToOne
    @JoinColumn(name = "ratingId")
    private Rating rating;

    // getters and setters
    }


    Post.java



    @Entity(name = "posts")
    public class Post {
    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    // irrelevant fields

    @ManyToMany
    @JoinTable(
    name = "users_posts_ratings_map",
    joinColumns = { @JoinColumn(name = "ratingId") },
    inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
    )
    private Set<UserPostRating> ratings = new HashSet<>();

    // getters and setters
    }


    I am getting



    org.hibernate.MappingException: Foreign key (FKB278E73083D94769:users_posts_ratings_map [postId,userId])) must have same number of columns as the referenced primary key (users_posts_ratings_map [ratingId,postId,userId])


    on servlet container initialization stage.



    What does it mean (What are Foreign Keys in this mappings? What are Primary Keys? Which annotations are marking what?) and how it could be fixed?










    share|improve this question

























      1












      1








      1








      Having a following SQL table:



      create table users_posts_ratings_map (
      postId integer not null references posts (id),
      userId integer not null references users (id),
      ratingId integer not null references ratings (id),
      primary key (postId, userId)
      );


      and Following JPA-Annotated POJOs:



      RatingId.java:



      @Embeddable
      public class RatingId implements Serializable {
      @ManyToOne
      @JoinColumn(name = "userId")
      private User user;

      @ManyToOne
      @JoinColumn(name = "postId")
      private Post post;

      // getters and setters
      }


      UserPostRating.java:



      @Entity(name = "users_posts_ratings_map")
      public class UserPostRating {
      @EmbeddedId
      private RatingId userPost;

      @OneToOne
      @JoinColumn(name = "ratingId")
      private Rating rating;

      // getters and setters
      }


      Post.java



      @Entity(name = "posts")
      public class Post {
      @Id
      @Column(nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;

      // irrelevant fields

      @ManyToMany
      @JoinTable(
      name = "users_posts_ratings_map",
      joinColumns = { @JoinColumn(name = "ratingId") },
      inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
      )
      private Set<UserPostRating> ratings = new HashSet<>();

      // getters and setters
      }


      I am getting



      org.hibernate.MappingException: Foreign key (FKB278E73083D94769:users_posts_ratings_map [postId,userId])) must have same number of columns as the referenced primary key (users_posts_ratings_map [ratingId,postId,userId])


      on servlet container initialization stage.



      What does it mean (What are Foreign Keys in this mappings? What are Primary Keys? Which annotations are marking what?) and how it could be fixed?










      share|improve this question














      Having a following SQL table:



      create table users_posts_ratings_map (
      postId integer not null references posts (id),
      userId integer not null references users (id),
      ratingId integer not null references ratings (id),
      primary key (postId, userId)
      );


      and Following JPA-Annotated POJOs:



      RatingId.java:



      @Embeddable
      public class RatingId implements Serializable {
      @ManyToOne
      @JoinColumn(name = "userId")
      private User user;

      @ManyToOne
      @JoinColumn(name = "postId")
      private Post post;

      // getters and setters
      }


      UserPostRating.java:



      @Entity(name = "users_posts_ratings_map")
      public class UserPostRating {
      @EmbeddedId
      private RatingId userPost;

      @OneToOne
      @JoinColumn(name = "ratingId")
      private Rating rating;

      // getters and setters
      }


      Post.java



      @Entity(name = "posts")
      public class Post {
      @Id
      @Column(nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;

      // irrelevant fields

      @ManyToMany
      @JoinTable(
      name = "users_posts_ratings_map",
      joinColumns = { @JoinColumn(name = "ratingId") },
      inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
      )
      private Set<UserPostRating> ratings = new HashSet<>();

      // getters and setters
      }


      I am getting



      org.hibernate.MappingException: Foreign key (FKB278E73083D94769:users_posts_ratings_map [postId,userId])) must have same number of columns as the referenced primary key (users_posts_ratings_map [ratingId,postId,userId])


      on servlet container initialization stage.



      What does it mean (What are Foreign Keys in this mappings? What are Primary Keys? Which annotations are marking what?) and how it could be fixed?







      java hibernate jpa orm hibernate-mapping






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 5 '13 at 13:52









      Alexander TuminAlexander Tumin

      8061530




      8061530
























          3 Answers
          3






          active

          oldest

          votes


















          4














          This mapping doesn't make much sense. You have an entity UserPostRating, mapped to the users_posts_ratings_map, and having a ManyToOne association with the entity Post.



          And in Post, you have a set of UserPostRating, but you map it as a second association, and make it a ManyToMany. It isn't a ManyToMany. It's a OneToMany, since the other side is a ManyToOne. And since the bidirectional association is already mapped in UserPostRating, you can't map it a second time in Post. So the code should be:



          @OneToMany(mappedBy="userPost.post")
          private Set<UserPostRating> ratings = new HashSet<>();





          share|improve this answer































            1














            According to error message I suspect, that have to move definition of



            @OneToOne
            @JoinColumn(name = "ratingId")
            private Rating rating;


            from class UserPostRating to class RatingId.






            share|improve this answer































              0














              The Mapping is correct, since it is a many to many mapping so its mapping will result to new table. So you should not refer to existing entity table, rather you should provide any other name whose mapping/entity name does not exist.
              Below is your example :



               @ManyToMany
              @JoinTable(
              name = "users_posts_ratings_map",
              joinColumns = { @JoinColumn(name = "ratingId") },
              inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
              )
              private Set<UserPostRating> ratings = new HashSet<>();


              Change the name from "users_posts_ratings_map" to any other name like users_posts_ratings_map1 or users_posts_ratings_map_item.






              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',
                autoActivateHeartbeat: false,
                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%2f14172401%2forg-hibernate-mappingexception-foreign-key-xxx-must-have-same-number-of-columns%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                This mapping doesn't make much sense. You have an entity UserPostRating, mapped to the users_posts_ratings_map, and having a ManyToOne association with the entity Post.



                And in Post, you have a set of UserPostRating, but you map it as a second association, and make it a ManyToMany. It isn't a ManyToMany. It's a OneToMany, since the other side is a ManyToOne. And since the bidirectional association is already mapped in UserPostRating, you can't map it a second time in Post. So the code should be:



                @OneToMany(mappedBy="userPost.post")
                private Set<UserPostRating> ratings = new HashSet<>();





                share|improve this answer




























                  4














                  This mapping doesn't make much sense. You have an entity UserPostRating, mapped to the users_posts_ratings_map, and having a ManyToOne association with the entity Post.



                  And in Post, you have a set of UserPostRating, but you map it as a second association, and make it a ManyToMany. It isn't a ManyToMany. It's a OneToMany, since the other side is a ManyToOne. And since the bidirectional association is already mapped in UserPostRating, you can't map it a second time in Post. So the code should be:



                  @OneToMany(mappedBy="userPost.post")
                  private Set<UserPostRating> ratings = new HashSet<>();





                  share|improve this answer


























                    4












                    4








                    4







                    This mapping doesn't make much sense. You have an entity UserPostRating, mapped to the users_posts_ratings_map, and having a ManyToOne association with the entity Post.



                    And in Post, you have a set of UserPostRating, but you map it as a second association, and make it a ManyToMany. It isn't a ManyToMany. It's a OneToMany, since the other side is a ManyToOne. And since the bidirectional association is already mapped in UserPostRating, you can't map it a second time in Post. So the code should be:



                    @OneToMany(mappedBy="userPost.post")
                    private Set<UserPostRating> ratings = new HashSet<>();





                    share|improve this answer













                    This mapping doesn't make much sense. You have an entity UserPostRating, mapped to the users_posts_ratings_map, and having a ManyToOne association with the entity Post.



                    And in Post, you have a set of UserPostRating, but you map it as a second association, and make it a ManyToMany. It isn't a ManyToMany. It's a OneToMany, since the other side is a ManyToOne. And since the bidirectional association is already mapped in UserPostRating, you can't map it a second time in Post. So the code should be:



                    @OneToMany(mappedBy="userPost.post")
                    private Set<UserPostRating> ratings = new HashSet<>();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 5 '13 at 14:11









                    JB NizetJB Nizet

                    544k588881013




                    544k588881013

























                        1














                        According to error message I suspect, that have to move definition of



                        @OneToOne
                        @JoinColumn(name = "ratingId")
                        private Rating rating;


                        from class UserPostRating to class RatingId.






                        share|improve this answer




























                          1














                          According to error message I suspect, that have to move definition of



                          @OneToOne
                          @JoinColumn(name = "ratingId")
                          private Rating rating;


                          from class UserPostRating to class RatingId.






                          share|improve this answer


























                            1












                            1








                            1







                            According to error message I suspect, that have to move definition of



                            @OneToOne
                            @JoinColumn(name = "ratingId")
                            private Rating rating;


                            from class UserPostRating to class RatingId.






                            share|improve this answer













                            According to error message I suspect, that have to move definition of



                            @OneToOne
                            @JoinColumn(name = "ratingId")
                            private Rating rating;


                            from class UserPostRating to class RatingId.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 5 '13 at 14:10









                            AndremoniyAndremoniy

                            22.1k675163




                            22.1k675163























                                0














                                The Mapping is correct, since it is a many to many mapping so its mapping will result to new table. So you should not refer to existing entity table, rather you should provide any other name whose mapping/entity name does not exist.
                                Below is your example :



                                 @ManyToMany
                                @JoinTable(
                                name = "users_posts_ratings_map",
                                joinColumns = { @JoinColumn(name = "ratingId") },
                                inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
                                )
                                private Set<UserPostRating> ratings = new HashSet<>();


                                Change the name from "users_posts_ratings_map" to any other name like users_posts_ratings_map1 or users_posts_ratings_map_item.






                                share|improve this answer






























                                  0














                                  The Mapping is correct, since it is a many to many mapping so its mapping will result to new table. So you should not refer to existing entity table, rather you should provide any other name whose mapping/entity name does not exist.
                                  Below is your example :



                                   @ManyToMany
                                  @JoinTable(
                                  name = "users_posts_ratings_map",
                                  joinColumns = { @JoinColumn(name = "ratingId") },
                                  inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
                                  )
                                  private Set<UserPostRating> ratings = new HashSet<>();


                                  Change the name from "users_posts_ratings_map" to any other name like users_posts_ratings_map1 or users_posts_ratings_map_item.






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    The Mapping is correct, since it is a many to many mapping so its mapping will result to new table. So you should not refer to existing entity table, rather you should provide any other name whose mapping/entity name does not exist.
                                    Below is your example :



                                     @ManyToMany
                                    @JoinTable(
                                    name = "users_posts_ratings_map",
                                    joinColumns = { @JoinColumn(name = "ratingId") },
                                    inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
                                    )
                                    private Set<UserPostRating> ratings = new HashSet<>();


                                    Change the name from "users_posts_ratings_map" to any other name like users_posts_ratings_map1 or users_posts_ratings_map_item.






                                    share|improve this answer















                                    The Mapping is correct, since it is a many to many mapping so its mapping will result to new table. So you should not refer to existing entity table, rather you should provide any other name whose mapping/entity name does not exist.
                                    Below is your example :



                                     @ManyToMany
                                    @JoinTable(
                                    name = "users_posts_ratings_map",
                                    joinColumns = { @JoinColumn(name = "ratingId") },
                                    inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
                                    )
                                    private Set<UserPostRating> ratings = new HashSet<>();


                                    Change the name from "users_posts_ratings_map" to any other name like users_posts_ratings_map1 or users_posts_ratings_map_item.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 28 '18 at 3:30









                                    TeeKea

                                    3,22851832




                                    3,22851832










                                    answered Nov 28 '18 at 2:24









                                    Manmaya ChampatirayManmaya Champatiray

                                    1




                                    1






























                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14172401%2forg-hibernate-mappingexception-foreign-key-xxx-must-have-same-number-of-columns%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

                                        A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                        Calculate evaluation metrics using cross_val_predict sklearn

                                        Insert data from modal to MySQL (multiple modal on website)