java - hibernate & jpa:具有复合主键的表:自动增量问题
问题描述
我有那些实体:
@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
您拥有@Id
3 个字段时,这就是您获得复合键的原因。像这样删除@Id
:station
carburant
@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();
}
}
推荐阅读
- swift - 访问 NSTextView 指标
- javascript - 使用 chrome.tabs.executeScript 时如何停止 chrome 在每次点击时注入相同的脚本
- java - Lombok 不创建构造函数
- python - GluonCV - 将对象检测模型导出到 Android
- r - 如何为R中另一列的每个级别创建一列?
- python-3.x - 是否有可能在 python3.x 中创建视觉碎片整理脚本?
- angular - 具有反应形式补丁值的 NgRx
- parsing - 使用 yysindex 和 yyrindex 的 YACC 表达式的含义
- python - 变量的值不会在嵌套循环中递增(使用 Python 编写文件)
- android - 在某些设备上使用 webview 小部件打开屏幕时出现 NullPointerException