帮助我理解如何使用hibernate实现多列自我加入额外列?我也应该映射连接表吗?或者可能存在另一种方式?我找不到任何有用的东西......
答案 0 :(得分:1)
将连接表映射为专用实体,然后通过两个OneToMany
关系链接它。这通常是正确的方式,因为只要您添加更多列,它就不仅仅是技术细节。
对于自联接,这应该采用相同的方式,在该模型上只有两个与加入实体关联的字段。
请参阅this answer,其中详细介绍了这一点。
答案 1 :(得分:0)
这个问题我纠结了很久,也许有人会觉得Wolfram的用词很难,所以我再详细解释一下: 想象一下,我们需要什么地图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 可以从数据库生成模型,这可能对您有所帮助Generate Persistence Mapping by Database Schema - Detail settings for Entity Class。 (此代码由它生成)