For example, A Users entity has a friends property, how can I design this friends property, in my thought there are 2 ways:
例如,一个用户实体有一个朋友属性,我如何设计这个朋友属性,在我看来有两种方式:
- friends property is a String with all usernames splitted by "," in this way it's hard to read and modify.
- friends属性是一个字符串,它的所有用户名都被“,”分割,因此很难读取和修改。
- friends property is a Set like
Set<Users>
, but in this way I don't know how to write in entities? - 朋友属性是一个集 <用户> ,但这样我不知道如何在实体中写?
Anyone knows the best practise?
有人知道最好的练习吗?
2 个解决方案
#1
1
If a User
can have multiple friends you could annotate your User
entity like this:
如果一个用户可以有多个朋友,您可以这样注释您的用户实体:
@Entity
public class User
{
@Id
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "user_friends",
joinColumns =
{ @JoinColumn(
name = "user_id") },
inverseJoinColumns =
{ @JoinColumn(
name = "friend_id") })
private Set<User> friends;
}
This way a table will get created for User
and a join table for the relationship between User
s. The User
table will have 2 columns, 'id' and 'name'. The user_friend
table will have 2 columns, 'user_id' and 'friend_id'. The columns in user_friend
are both foreign keys to the User
table.
这样,将为用户创建一个表,为用户之间的关系创建一个连接表。用户表将有两列,“id”和“name”。user_friend表将有两个列“user_id”和“friend_id”。user_friend中的列都是用户表的外键。
#2
2
This is covered Enterprise Model Patterns by Hay.
这是由Hay覆盖的企业模型模式。
A party represents a person (or an organization):
一方代表一个人(或组织):
Party
id
name
A party can have a relationship to another party, over a time period:
一方可以与另一方有一段时间的关系:
PartyRelationship
fromPartyId
toPartyId
fromDate
toDate nullable
A basic diagram:
一个基本的图:
Party -< PartyRelationship >- Party
Sample SQL:
示例SQL:
insert into party values (1, 'Jerry');
insert into party values (2, 'Neil');
insert into partyRelationship values (1, 2, getDate(), null);
#1
1
If a User
can have multiple friends you could annotate your User
entity like this:
如果一个用户可以有多个朋友,您可以这样注释您的用户实体:
@Entity
public class User
{
@Id
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "user_friends",
joinColumns =
{ @JoinColumn(
name = "user_id") },
inverseJoinColumns =
{ @JoinColumn(
name = "friend_id") })
private Set<User> friends;
}
This way a table will get created for User
and a join table for the relationship between User
s. The User
table will have 2 columns, 'id' and 'name'. The user_friend
table will have 2 columns, 'user_id' and 'friend_id'. The columns in user_friend
are both foreign keys to the User
table.
这样,将为用户创建一个表,为用户之间的关系创建一个连接表。用户表将有两列,“id”和“name”。user_friend表将有两个列“user_id”和“friend_id”。user_friend中的列都是用户表的外键。
#2
2
This is covered Enterprise Model Patterns by Hay.
这是由Hay覆盖的企业模型模式。
A party represents a person (or an organization):
一方代表一个人(或组织):
Party
id
name
A party can have a relationship to another party, over a time period:
一方可以与另一方有一段时间的关系:
PartyRelationship
fromPartyId
toPartyId
fromDate
toDate nullable
A basic diagram:
一个基本的图:
Party -< PartyRelationship >- Party
Sample SQL:
示例SQL:
insert into party values (1, 'Jerry');
insert into party values (2, 'Neil');
insert into partyRelationship values (1, 2, getDate(), null);