gpt4 book ai didi

java - 三路 hibernate ORM 映射 - 如何?

转载 作者:行者123 更新时间:2023-11-30 04:33:08 25 4
gpt4 key购买 nike

我有三个 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
);

一个帖子只能有一个作者,因此 users <-> posts 关系已经以正常形式建立(如果我错了,请纠正我)。

评级是预定义的常量,例如“坏”、“好”或“很棒”,以及(在实际情况中)附加数据作为评级值、描述或其他字段,为简洁起见,我在此处省略了这些字段。

接下来我想将评级与用户和帖子相关联。每个帖子可以由每个用户评级一次,并且可以由多个用户评级。我想出了以下关系:

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 映射中的方法,以获取每个帖子列表(或集合或任何其他集合)的(用户,评级)对。

这是我现在尝试绘制它们的方法:

用户.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
// ...
}

为了将评级和用户对列表与每个帖子相关联,需要更改哪些内容?

UPD1

明显错误:posts_ ratings_users_map 的 PK 应该是 (postId, userId)(不包括 ratingId),否则同一用户可以对同一个帖子进行不同的评分。

最佳答案

也许稍微改变一下你的模型。

用户拥有您所说的已定义的帖子。

为什么不创建一个新的实体“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 而不是 ManyToMany 具有 OneToMany 关系。使用评级和用户的 id 作为 UserRating 类的键会更理想,但这在 JPA/Hibernate 中不容易建模,这使得它非常复杂。如果您有兴趣,请看看这些问题

Mapping ManyToMany with composite Primary key and Annotation

Many to many hibernate mapping with extra columns?

关于java - 三路 hibernate ORM 映射 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com