Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:58:14+00:00 2026-06-17T04:58:14+00:00

I have a three SQL tables: create table users ( id serial primary key,

  • 0

I have a three SQL tables:

create table users (
  id serial primary key,
  name text not null unique
);

create table posts (
  id serial primary key,
  data text not null,
  authorId integer not null references users (id)
);

create table ratings (
  id serial primary key,
  name text not null unique
);

One post can have only one author, so users <-> posts relation is already established in normal form (correct me if i am wrong).

Ratings are pre-defined constants like “bad”, “good” or “awesome”, with (in real case) additional data as rating value, description or other fields i omitted here for brevity.

Next i want to relate ratings to users and posts. Each post may be rated once by each user, and may be rated by multiple users. I came up with the following relation:

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

But here is the problem: i can’t see a way to integrate it within Hibernate ORM mapping, to get for each of posts list (or set, or any other collection) of pairs of (User,Rating).

Here is how i am trying to map them now:

User.java:

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

    @Column(nullable = false)
    private String name;

    @OneToMany
    @JoinColumn(name = "authorId")
    private Set<Post> posts = new HashSet<>();

    // ...
    // getters and setters
    // ...
}

Rating.java:

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

    @Column(nullable = false)
    private String name;

    @ManyToMany(mappedBy = "ratings")
    private Set<Post> posts = new HashSet<>();

    // ...
    // getters and setters
    // ...
}

Post.java:

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

    @Column(nullable = false)
    private String data;

    @ManyToOne
    @JoinColumn(name = "authorId")
    private User author;

    @ManyToMany
    @JoinTable(
            name = "posts_ratings_users_map",
            joinColumns = { @JoinColumn(name = "ratingId") },
            inverseJoinColumns = { @JoinColumn(name = "postId") }
    )
    private Set<Rating> ratings = new HashSet<>(); // here is the problem. i can relate ratings to this post, but how
                                                   // do i relate them with users which assigned their ratings to this
                                                   // post ?


    // ...
    // getters and setters
    // ...
}

What needs to be changed in order to relate list of pairs of rating&user to each post?

UPD1

Obvious error: PK for posts_ratings_users_map should be (postId, userId) (excluding ratingId), otherwise same user was able to put different ratings on the same post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:58:16+00:00Added an answer on June 17, 2026 at 4:58 am

    Maybe change your model a little.

    Users have posts which you said is already defined.

    Why not create a new Entity ‘UserRating’.

    @Entity(name = "user_ratings")
    public class UserRating {
        @Id
        @Column(nullable = false)
        @GeneratedValue
        private Integer id;
    
        @ManyToOne(nullable = false)
        @JoinColumn(name = "ratingId")
        private Rating rating;
    
        @ManyToOne(nullable = false)
        @JoinColumn(name = "authorId")
        private User ratedBy;
     }
    

    Now on your Post instead of the ManyToMany have a OneToMany relationship. It would be more ideal to use the id of the rating and the user as the key for the UserRating class but this doesnt model easily in JPA/Hibernate which makes it quite complex. If your interested have a look at these questions

    Mapping ManyToMany with composite Primary key and Annotation

    Many to many hibernate mapping with extra columns?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have three tables: CREATE TABLE tblUser ( [pkUserID] [int] IDENTITY(1,1) NOT NULL,
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
I have three tables: CREATE TABLE Movie ( movieId INTEGER GENERATED BY DEFAULT AS
I am using SQL Server 2005. I have three tables - Users, Groups, and
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have defined a table type PL/SQL variable and added some data there. create
I have a Microsoft SQL Server 2008 query that returns data from three tables
I'm using SQL Server 2008 and I have a table with three columns: Length
I have a tree structure in an sql table like so: CREATE TABLE containers
I have three tables [USER] --Master user table [KEYWORD] --Master keyword table [USER_KEYWORD] --[USER]-[KEYWORD]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.