帮助我了解如何使用休眠实现多对多自连接和额外的列?我也应该映射连接表吗?或者可能以另一种方式存在?我找不到任何对我有用的东西......
问问题
2076 次
2 回答
1
将连接表映射为专用实体,然后通过两个OneToMany
关系链接它。这通常是正确的方法,因为一旦您添加更多列,它就不仅仅是技术细节。
对于自联接,这应该以相同的方式工作,您在该模型上只有两个与联接实体关联的字段。
请参阅此答案,其中对此进行了更详细的描述。
于 2012-07-11T14:24:52.460 回答
0
这个问题我纠结了很久,也许有人会觉得 Wolfram 的措辞很难,所以我会更详细地解释一下:想象一下,我们需要什么 map 之类的uml_diagramm
我需要在任意两个用户之间建立链接,这些链接用于他们之间的聊天。所以我们的用户类将是:
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Entity
public class User implements Serializable {
private String email;
private String password;
private Long id;
private String nickname;
private String phone;
private Collection<Chats> chatsById;
private Collection<Chats> chatsById_0;
@Basic
@Column(name = "email", nullable = false, length = -1)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Basic
@Column(name = "password", nullable = false, length = -1)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
@Column(name = "id", nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Basic
@Column(name = "nickname", nullable = true, length = -1)
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Basic
@Column(name = "phone", nullable = true, length = -1)
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(email, user.email) && Objects.equals(password, user.password) && Objects.equals(id, user.id) && Objects.equals(nickname, user.nickname) && Objects.equals(phone, user.phone);
}
@Override
public int hashCode() {
return Objects.hash(email, password, id, nickname, phone);
}
@OneToMany(mappedBy = "userByIdFrom")
public Collection<Chats> getChatsById() {
return chatsById;
}
public void setChatsById(Collection<Chats> chatsById) {
this.chatsById = chatsById;
}
@OneToMany(mappedBy = "userByIdTo")
public Collection<Chats> getChatsById_0() {
return chatsById_0;
}
public void setChatsById_0(Collection<Chats> chatsById_0) {
this.chatsById_0 = chatsById_0;
}
}
请注意两个 @OneToMany 注释 - getChatsById 和 getChatsById_0
现在我们的聊天类:
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Entity
@IdClass(ChatsPK.class)
public class Chats implements Serializable {
private Long idFrom;
private Long idTo;
private Object uuid;
private User userByIdFrom;
private User userByIdTo;
@Id
@Column(name = "id_from", nullable = false)
public Long getIdFrom() {
return idFrom;
}
public void setIdFrom(Long idFrom) {
this.idFrom = idFrom;
}
@Id
@Column(name = "id_to", nullable = false)
public Long getIdTo() {
return idTo;
}
public void setIdTo(Long idTo) {
this.idTo = idTo;
}
@Basic
@Column(name = "uuid", nullable = false)
public Object getUuid() {
return uuid;
}
public void setUuid(Object uuid) {
this.uuid = uuid;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Chats chats = (Chats) o;
return Objects.equals(idFrom, chats.idFrom) && Objects.equals(idTo, chats.idTo) && Objects.equals(uuid, chats.uuid);
}
@Override
public int hashCode() {
return Objects.hash(idFrom, idTo, uuid);
}
@ManyToOne
@JoinColumn(name = "id_from", referencedColumnName = "id", nullable = false)
public User getUserByIdFrom() {
return userByIdFrom;
}
public void setUserByIdFrom(User userByIdFrom) {
this.userByIdFrom = userByIdFrom;
}
@ManyToOne
@JoinColumn(name = "id_to", referencedColumnName = "id", nullable = false)
public User getUserByIdTo() {
return userByIdTo;
}
public void setUserByIdTo(User userByIdTo) {
this.userByIdTo = userByIdTo;
}
}
老实说,我自己还没有弄清楚为什么它会起作用,但是应该有更多的细节
同时,我知道 intelijIdea 可以从数据库中为您生成模型,这可能有助于您通过 Database Schema - Entity Class 的详细设置生成持久性映射。(此代码由它生成)
于 2021-03-19T17:58:02.683 回答