我正在研究双向Map。我用@OneToOne和@ManyToMany注解Map了Team和Member。
@Entity
public class Team {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members=new ArrayList<Member>();
//omit getter, setter ,toString
}
@Entity
public class Member {
@Id
@GeneratedValue
private Long id;
@Column(name="USERNAME")
private String name;
@ManyToOne
@JoinColumn(name="TEAM_ID")
private Team team;
@Enumerated(EnumType.STRING)
private Status status;
//omit getter, setter , toString
}
主方法
public static void main(String args[]){
//...
Team team= new Team();
team.setName("RedTeam");
em.persist(team);
Member member= new Member();
member.setName("me");
member.setStatus(Status.ADMIN);
member.setTeam(team);
em.persist(member);
Member findmember= em.find(Member.class, member.getId());
Team findTeam= findmember.getTeam();
System.out.println("members: "+findTeam.getMembers());
//...
}
结果:
成员:[]
我想知道为什么在上面的代码中没有将“members”添加到“Team”的“members field”中。
1条答案
按热度按时间tp5buhyn1#
这些是使其正常工作所需的最小更改