[英]hibernate & jpa : table with composite primary key : auto increment problem
我有那些實體:
@Entity
public class Carburant implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_carburant")
private long id;
private String nom;
private String description;
@JsonIgnore
@OneToMany(mappedBy="carburant")
private Set<HistCarb> stations ;
public Carburant() {
super();
}
}
2
@Entity
@Table(name="Station")
public class Station implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_station")
private long id ;
private String nom;
private String ville;
private String adresse;
@Transient
private boolean nul = false;
@JsonIgnore
@OneToMany(mappedBy="station")
private Set<HistCarb> historiques ;
public Station() {
super();
}
}
3
@Entity
public class HistCarb implements Serializable{
@Id
@Column(name="id",updatable=false,nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private Date date;
private Integer prix;
@Id
@ManyToOne
@JoinColumn(name="id_station")
private Station station ;
@Id
@ManyToOne
@JoinColumn(name="id_carburant")
private Carburant carburant ;
public HistCarb() {
super();
}
}
類圖: 在此處輸入圖像描述
而她的問題是:休眠給我表HistCarb的此sql代碼:
create table HistCarb (
id bigint not null,
date datetime,
prix integer,
id_station bigint not null auto_increment,
id_carburant bigint not null,
primary key (id_station, id, id_carburant)
) engine=InnoDB
與id_station auto_increment一起使用,但是我想讓休眠狀態僅將列ID作為auto_increment字段生成,就像我在實體3中提到的那樣,我希望有人可以幫助我解決這個問題。 我沒有為實體3使用嵌入式ID,我認為我們可以在沒有嵌入式ID的情況下完成它,因為我發現實現起來非常困難,當我在這種情況下嘗試使用嵌入式ID時,會出現一些錯誤。
在HistCarb
您在3個字段上都有@Id
,這就是為什么要獲取復合鍵的原因。 像這樣從station
和化carburant
刪除@Id
:
@Entity
public class HistCarb implements Serializable{
@Id
@Column(name="id",updatable=false,nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private Date date;
private Integer prix;
@ManyToOne
@JoinColumn(name="id_station")
private Station station ;
@ManyToOne
@JoinColumn(name="id_carburant")
private Carburant carburant ;
public HistCarb() {
super();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.