gpt4 book ai didi

java - JPA + Hibernate + Spring - 父子持久化生成 ID 导致外键错误

转载 作者:行者123 更新时间:2023-11-30 02:53:26 26 4
gpt4 key购买 nike

在 IntelliData 回答后,我编辑了我的问题。

数据库

在我拥有的数据库中,我无法进行任何更改:

Table A {
ID NUMBER(10, 0) NOT NULL
DATE_DEBUT DATE NOT NULL
DATE_FIN DATE
.....
}

Table B {
ID NUMBER(10, 0) NOT NULL
B_ID VARCHAR2(10 BYTE) NOT NULL
}

表A中的ID是PK,它是一个生成的值。
B表中(ID,B_ID)为PK。
B.ID 在 A.ID 上是 FK。

实体

一个

@Entity
@Table(name = "A")
public class A implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "aIdGenerator")
@SequenceGenerator(name = "aIdGenerator", sequenceName = "A_SEQ", allocationSize = 1)
@Column(name = "ID")
private Integer id;

@Column(name = "DATE_DEBUT")
private LocalDate dateDebut;

@Column(name = "DATE_FIN")
private LocalDate dateFin;

@OneToMany(mappedBy = "a", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST })
private List<B> bs;
.
.
.
}

B

@Entity
@Table(name = "B")
public class B implements Serializable {

@Id
@ManyToOne
@JoinColumn(name = "ID", referencedColumnName = "ID")
private A a;

@Id
@Column(name = "B_ID")
private String bID;

public B(final A a, final String bID) {
super();
this.a = a
this.bID= bID;
}
.
.
.
}

存储库

public interface aRepository extends JpaRepository<A, Integer> {
// nothing more
}

我的java

A a= new A();
a.setDateDebut(LocalDate.now());
a.setDateFin(LocalDate.now());

B bs = new ArrayList<B>();
bs.add(new B(a, "aaa"));
bs.add(new B(a, "bbb"));
bs.add(new B(a, "ccc"));

a.setBs(bs);

myrepo.saveAndFlush(a);

问题(最终)

ORA-01400: cannot insert NULL into ("B"."ID") ---> RESOLVED by IntelliData

但现在我明白了

ORA-02291: integrity constraint (FK_B_ID) violated - parent key not found

我不明白为什么,因为 Hibernate 以良好的顺序插入并且具有良好的值...我设法获取了这些日志:

DEBUG [main] org.hibernate.SQL(109) - insert into GOELAND_DEPLOI (DATE_DEBUT, DATE_FIN, ID) values (?, ?, ?)
Hibernate: insert into A (DATE_DEBUT, DATE_FIN, ID) values (?, ?, ?)
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [1] as [DATE] - [2016-06-22]
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [1] as [DATE] - [2016-06-22]
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [2] as [DATE] - [2016-06-22]
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [2] as [DATE] - [2016-06-22]
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [3] as [INTEGER] - [127]
DEBUG [main] org.hibernate.SQL(109) - insert into B (ID, B_ID) values (?, ?)
Hibernate: insert into B (ID, B_ID) values (?, ?)
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [1] as [INTEGER] - [127]
TRACE [main] o.h.t.d.sql.BasicBinder(81) - binding parameter [2] as [VARCHAR] - [TEST]
DEBUG [main] o.h.e.j.spi.SqlExceptionHelper(139) - could not execute statement [n/a]
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: violation de contrainte d'intégrité (COMMON.FK_B_ID) - clé parent introuvable

如果还不够,我可以添加更多出现在 myrepo.saveAndFlush(a); 上的日志

最佳答案

为了使 b.ID 具有值,您必须将 a 添加到 b,如下所示:

b.setA(a);

B 的构造函数设置字段 b.bId,但不设置字段 b.a ...

<小时/>

更新:

尝试更改此设置:

@OneToMany(mappedBy = "a", fetch = FetchType.LAZY, cascade = {   CascadeType.PERSIST })
private List<B> bs;

对此:

@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name="ID")
@NotNull
private List<B> b;

类似于this上的答案邮政。如果有效,请告诉我。

关于java - JPA + Hibernate + Spring - 父子持久化生成 ID 导致外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37949705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com