、请教一个关于hibernate注解外间关联的问题,为什么外键关联只能关联主键
有两个类,一个Group类,@Entity@Table(name="t_group")publicclassGroup{privateintid;privateStrin...
有两个类,一个Group类,
@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
private Set<User> user=new HashSet<User>();
public void setId(int id) {
this.id = id;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUsers(Set<User> users) {
this.user = users;
}
@OneToMany
@JoinColumn(name ="groupId")
public Set<User> getUsers() {
return user;
}
}
一个User类
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
group和user是一对多的关系,我想问的是,为什么在使用外键关联时,只能关联主键id,能不能不关联主键,比如关联name 展开
@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
private Set<User> user=new HashSet<User>();
public void setId(int id) {
this.id = id;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUsers(Set<User> users) {
this.user = users;
}
@OneToMany
@JoinColumn(name ="groupId")
public Set<User> getUsers() {
return user;
}
}
一个User类
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
group和user是一对多的关系,我想问的是,为什么在使用外键关联时,只能关联主键id,能不能不关联主键,比如关联name 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询