Hibernate不合并条目

时间:2016-05-17 12:35:35

标签: java mysql spring hibernate jpa

我尝试在表中插入多个时间戳' time'。每个时间戳都包含竞争对手'中的条目ID。表。每当我在我的数据库中创建一个新的时间戳时,我也会获得一个新的竞争者条目。

你能告诉我为什么我总会得到一个新的条目,我该如何防止这种情况?

竞争

@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 个答案:

答案 0 :(得分:1)

您需要@OneToOne注释,例如:

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