我有这些实体:
@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();
}
}
类图:在此处输入图像描述
问题是:hibernate给我这个表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,但我希望hibernate只生成列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();
}
}