[英]hibernate secondary table for same entity relationship
我有一個包含用戶信息的 Profile 類。 此應用程序的功能之一是“關注”您選擇的另一個配置文件。 我在設計數據庫和建立鏈接時遇到了麻煩,包括編寫模型代碼和存儲庫(這只是保存的問題嗎?)。
我的 Profile 類看起來像這樣:(為了簡單起見,省略了與問題和 getter/setter 無關的字段)
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.*;
@Entity
@Table(name = "USERS")
public class Profile implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(max = 32)
@Column(nullable = false)
private String name;
@Size(max = 16)
@Column(unique=true, nullable = false)
private String username;
@Email
@Column(unique=true)
private String email;
@ManyToOne
@JoinColumn(name="profile_id")
private Profile profile;
@OneToMany(mappedBy = "profile")
private List<Profile> friends = new ArrayList<>();
public Profile() {
}
// other getters/setters
public List<Profile> getFriends() {
return friends;
}
public void setFriends(List<Profile> friends) {
this.friends = friends;
}
public void addFriend(Profile friend) {
this.friends.add(friend);
}
}
這會在用戶表中創建一個名為profile_id
的null
字段。 我做了一些研究,似乎使用@SecondaryTable
注釋創建另一個表可能會成功。 我研究過的教程並沒有解決我的疑慮。 我想對此事有所了解,如果可能,請避免創建不同的實體。
理想情況下,我有一個表,有自己的 id、所有者的 id(關注的人)和目標的 id(被關注的人)。
提前致謝!
您需要的是一個單獨的表來存儲以下關系(多對多)。 最簡單的方法是使用@ManyToMany
:
@Entity
@Table(name = "USERS")
public class Profile implements Serializable {
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="following",
joinColumns={@JoinColumn(name="user_id")},
inverseJoinColumns={@JoinColumn(name="follower_id")})
private Set<Profile> followers = new HashSet<Profile>();
@ManyToMany(mappedBy = "followers", cascade = CascadeType.ALL)
private Set<Profile> followBy = new HashSet<Profile>();
}
它將被映射到一個名為following
的關系表,該表具有以下架構:
---------------------------------
| column | FK |
=================================
| user_id | FK to users.id |
---------------------------------
| follower_id | FK to users.id |
---------------------------------
因此,給定用戶 A ,字段followers
是 A followers
的所有用戶。 字段followBy
是所有關注 A 的用戶。
此外,注意mappedBy
設置。它意味着你必須使用字段followers
保持這種關系之后而不是場followBy
,你必須插入這基本上意味着/刪除實例的followers
設置更改為以下關系給定的個人資料。
如果您想在關系表中添加額外的列(即following
表),也可以看到這個
好吧,實現@ManyToMany
關系有兩種方法。 首先是創建一個單獨的表,僅包含每個實體的相應 Id。 而那件事,可能很容易,但它並沒有涵蓋所有情況。 例如,在您的情況下,您正在為用戶和關注者實施@ManyToMany
。 您可能還需要一個日期,以顯示他們創建該關系的日期。 這就是事情變得有點復雜(不多)的情況,但它導致了一段美麗的代碼。 無論如何,讓我們采用兩種方法:
`
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name = "profile_follower",
joinColumns = @JoinColumn(name = "profile_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name = "following_id", referencedColumnName="id")
private Set<Profile> profiles;
@ManyToMany(mappedBy = "profiles")
private Set<Profile> following= new HashSet<>();
`
另一種方法是為關系創建一個單獨的類。 `
@Entity
public class ProfilesFollowedByUser{
@EmbeddedId
UserFollower id;//this is a separate class
@ManyToOne
@MapsId("id")
@JoinColumn(name="profile_id", referencedColumnName="id")
private Profile profile;
@ManyToOne
@MapsId("id")
@JoinColumn(name="following_id", referencedColumnName="id")
private Profile profile;
private Date createdAt;}
` 這是可嵌入的類:
`
@Embeddable
public class UserFollower implements Serializable{
private static final long serialVersionUID = 819015237657421374L;
@Column(name="profile_id")
private Long profileId;
@Column(name="following_id")
private Long followingId;
}
` 第二個可能有點復雜,但在絕大多數情況下,至少在現實世界的應用程序中,當你必須處理一個關系時,至少總是需要創建一個日期。 希望對您的問題有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.