简体   繁体   中英

Hibernate does not merge entry

i try to insert multiple timestamps to the table 'time'. Each timestamp contains the id of an entry in the 'competitor' table. Everytime I create a new timestamp in my Database, i also get a new competitor entry.

Could you please tell me why i always get a new Entry and how can i prevent that?

Competitor

@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;
    }
}

Time

@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;
    }

}

You need the @OneToOne annotation, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM