gpt4 book ai didi

java - 未找到 ParentKey 具有 2 个外键的模型中出现错误

转载 作者:行者123 更新时间:2023-12-02 03:46:39 25 4
gpt4 key购买 nike

我有以下型号;

//A

class A {
@Id
private int id;
@Id
@Column(name = "D",
nullable = false,
precision = 20,
scale = 0
)
private Long dId;

@ManyToOne
@JsonManagedReference
@JoinColumn(
name = "D",
nullable = false,
insertable = false,
updatable = false
)
private D d;

@OneToMany(mappedby = "a",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<C> Cs;
}

//B

class B {
@Id
int id;
@Column(name = "D",
nullable = false,
precision = 20,
scale = 0
)
private Long dId;

@ManyToOne
@JsonManagedReference
@JoinColumn(
name = "D",
nullable = false,
insertable = false,
updatable = false
)
private D d;

@OneToMany(mappedby = "b",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<C> Cs;
}

//C

class C {
@Id
int id;
String name;
@ManyToOne
@JsonManagedReference
@JoinColumns(

{
@JoinColumn(name = "A", insertable = false, updatable = false),
@JoinColumn(name = "D", insertable = false, updatable = false)

}
)
private A a;

@ManyToOne
@JsonManagedReference
@JoinColumns(
{

@JoinColumn(name = "B", insertable = false, updatable = false),
@JoinColumn(name = "D", insertable = false, updatable = false)
}
)
private B B;


@Id
@Column(name = "D",
nullable = false,
precision = 20,
scale = 0)
private Long dId;

@Id
@Column(name = "B",
nullable = false,
precision = 3,
scale = 0)
private Integer bId;

@Id
@Column(name = "A",
nullable = false,
precision = 5,
scale = 0)
private Integer aId;
}

//D

 class D{
@Id
int id;

@OneToMany(mappedby = "d",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<A> As;
@OneToMany(mappedby = "d",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<B> Bs;
}

所以这里的一切都是通过D类来保存的。 (这意味着我设置了模型并通过 D 存储库保存。)我可以通过 D 保存对象列表 A 和 B 没有任何问题。(无需添加 C 对象) 。我通过对象 A 添加对象 C,因为 C 的真正关系是与 A 的关系。问题是当我通过 Object A 添加 Object C 时,spring 抛出以下异常;

Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (XYZ.RELATIONSHIP0123) violated - parent key not found

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1075)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3897)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1361)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 98 more

我发现 spring 首先保存 A 对象,然后在保存 B 对象之前移动到 C 对象,因此 spring 抛出上述错误当C对象保存时,没有Object B的条目。有什么办法可以解决这个问题吗?

PS:我没有任何权限更改数据库结构

PSS:这是C的表结构;

PK compositekey(A+B+C)
FK A (number)
FK B (number)
FK D (number)
lasteditedTime (time)

因此,我手动将 A、BD id 添加到 C 对象 并设置每个对象。

最佳答案

您的映射不正确。

您需要将@Id直接放在关系上:

https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

A的情况:

class A {
@Id
private int id;

//@Id
//@Column(name = "D")
//private Long dId;

@Id //added
@ManyToOne
@JsonManagedReference
@JoinColumn(
name = "D",
nullable = false,
insertable = false,
updatable = false
)
private D d;

@OneToMany(mappedby = "a",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<C> Cs;
}

您需要在 C 中对与 B 的关系进行类似的更改。如果没有这些更改,Hibernate 就不知道 A 依赖于 D 且 C 依赖于 B 的身份。

您通常还需要创建一个 Id 或 EmbeddedID 类,但 Hibernate 允许多个 ID 字段,但无需此。

如果您想保留dId字段,您可以使用@MapsId。我认为这看起来像下面这样。

class A {
@Id
private int id;

@Id
@Column(name = "D")
private Long dId;

@MapsId("dId") //added
@ManyToOne
@JsonManagedReference
@JoinColumn(
name = "D",
nullable = false,
insertable = false,
updatable = false
)
private D d;

@OneToMany(mappedby = "a",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true )
Set<C> Cs;
}

关于java - 未找到 ParentKey 具有 2 个外键的模型中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56801857/

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