1

3 つの SQL テーブルがあります。

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
);

1 つの投稿には作成者が 1 人しかいないため、users<->posts関係は通常の形式で既に確立されています (間違っていたら訂正してください)。

評価は、「悪い」、「良い」、「素晴らしい」などの事前定義された定数であり、(実際の場合) 評価値、説明、またはその他のフィールドとして追加のデータがあり、簡潔にするためにここでは省略されています。

次に、評価をユーザーと投稿に関連付けたいと思います。各投稿は、各ユーザーが 1 回評価することも、複数のユーザーが評価することもできます。私は次の関係を思いついた:

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)
);

しかし、ここに問題があります: Hibernate ORM マッピング内に統合して、(User,Rating) のペアの各投稿リスト (またはセット、またはその他のコレクション) を取得する方法がわかりません。

これが私が今それらをマッピングしようとしている方法です:

ユーザー.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
    // ...
}

評価.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
    // ...
}

評価とユーザーのペアのリストを各投稿に関連付けるには、何を変更する必要がありますか?

UPD1

明らかなエラー: posts_ratings_users_map の PK は(postId, userId)(を除くratingId) にする必要があります。そうしないと、同じユーザーが同じ投稿に異なる評価を付けることができました。

4

2 に答える 2

2

モデルを少し変更するかもしれません。

ユーザーには、すでに定義されているとあなたが言った投稿があります。

新しいエンティティ「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;
 }

今、関係を持つPost代わりにあなたに。クラスのキーとして評価とユーザーのIDを使用する方が理想的ですが、これはJPA / Hibernateで簡単にモデル化できないため、非常に複雑になります。興味のある方はこちらの質問をご覧くださいManyToManyOneToManyUserRating

複合主キーと注釈を使用した ManyToMany のマッピング

追加の列を使用した多対多の休止状態マッピング?

于 2013-01-03T18:03:41.570 に答える
0

評価クラスにユーザーを含める

@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<>();

@Onetoone
private User ratedBy;

// ...
// getters and setters
// ...
}
于 2013-01-03T13:22:54.897 に答える