0
我有兩個實體,一個競爭和團隊:JPA多對一,而不是持續的一對多關係
比賽:
public void setNextCompetitionAttending(Competition nextCompetitionAttending) {
this.nextCompetitionAttending = nextCompetitionAttending;
nextCompetitionAttending.addTeam(this);
}
比賽:
@Entity
public class Team implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(length=255)
String name;
@ManyToOne(optional=true)
Competition nextCompetitionAttending;
List<SiteUser> teamMembers;
nextComeptitionAttending通過以下方法設置定義如下:
@Entity
public class Competition implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
int maxTeamSize;
String name;
@OneToMany(mappedBy="nextCompetitionAttending")
List<Team> teamsAttending;
我的持久性代碼如下所示:
utx.begin();
EntityManager em = emf.createEntityManager();
em.merge(user);
Competition c = em.find(Competition.class, id);
Team teamon = user.getTeamOn();
teamon.setNextCompetitionAttending(c);
System.out.println("Set next");
em.flush();
utx.commit();
em = emf.createEntityManager();
Team t = em.find(Team.class, teamon.getId());
System.out.println(t.getNextCompetitionAttending());
在數據庫中,列「NEXTCOMPETITIONATTENDING_ID」爲空,因爲是我一直在使用,試圖調試此代碼的最後println的。
這爲什麼不持續?
這條線? – Lion 2011-12-17 23:17:39
你收到了什麼錯誤? – Lion 2011-12-17 23:23:44
@Lion:IDE如何相關? Java代碼是Java代碼。無論是否使用NetBeans創建都無關緊要。 – 2011-12-17 23:26:50