多对一(many to one)
多对一的数据库设计原则:
//注意在创建实体类属性时应尽量避免与
用户和组 这里从用户看 多个用户只能对应一个组
只需在多的一端加@ManyToOne
@Entity
@Table(name = "t_user")
public class User {
private int id;
private String name;
private Group group;
@ManyToOne
@JoinColumn(name="groupId")//设置外键的名称
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "t_group")
public class Group {
private int id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}