2016-05-17 117 views
0

我嘗試插入多個時間戳到表'時間'。每個時間戳包含「競爭對手」表中的條目的標識。每當我在我的數據庫中創建一個新的時間戳時,我也會得到一個新的競爭對手條目。休眠不合並條目

你能告訴我爲什麼我總是得到一個新的條目,我該如何防止?

競爭對手

@Entity 
@Table(name="competitor") 
public class Competitor extends BaseEntity{ 

    private String firstName; 
    private String lastName; 
    private String birthday; 

    @Column(name="firstName") 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    @Column(name="lastName") 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    @Column(name="birthday") 
    public String getBirthday() { 
     return birthday; 
    } 
    public void setBirthday(String birthday) { 
     this.birthday = birthday; 
    } 
} 

時間

@Entity 
@Table(name="time") 
public class Time extends BaseEntity{ 

    private String route; 
    private Competitor competitor; 
    private String time; 

    @Column(name="route") 
    public String getRoute() { 
     return route; 
    } 
    public void setRoute(String route) { 
     this.route = route; 
    } 

    @ManyToOne(cascade = CascadeType.ALL) 
    public Competitor getCompetitor() { 
     return competitor; 
    } 
    public void setCompetitor(Competitor competitor) { 
     this.competitor = competitor; 
    } 

    @Column(name="time") 
    public String getTime() { 
     return time; 
    } 
    public void setTime(String time) { 
     this.time = time; 
    } 

} 
+1

如果你想預先存在的'Competitor'關聯到一個'時間「實體,您需要首先檢索」競爭者「,並在」時間「對象上設置檢索到的對象。 – Priyesh

回答

1

您需要@OneToOne註釋,例如:

@OneToOne 
@JoinColumn(name = "competitor_id") 
private Competitor competitor; 
+0

Thangs爲您提供幫助;)現在它工作 –