gpt4 book ai didi

java - 可为空到 true 的一对一关系以及使用主键的关系

转载 作者:行者123 更新时间:2023-12-01 14:22:12 25 4
gpt4 key购买 nike

我有两个具有 OneToOne 关系的实体:

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

[...]

@Id
public ApplicationDeviceKey getApplicationDeviceKey()
{
return applicationDeviceKey;
}

@NotFound(action = NotFoundAction.IGNORE)
@OneToOne(cascade=CascadeType.ALL)
@JoinColumns( {
@JoinColumn(name = "applicationId", referencedColumnName = "applicationId",insertable=false,updatable=false, nullable = true),
@JoinColumn(name = "deviceId", referencedColumnName = "deviceId",insertable=false,updatable=false, nullable = true), }
)
public ApplicationDevicePushInfo getDevicePushInfo() {
return devicePushInfo;
}

和另一个实体:

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

[...]
@Id
public ApplicationDeviceKey getApplicationDeviceKey()
{
return applicationDeviceKey;
}

@OneToOne
@JoinColumns( {
@JoinColumn(name = "applicationId", referencedColumnName = "applicationId",insertable=false,updatable=false, nullable = false),
@JoinColumn(name = "deviceId", referencedColumnName = "deviceId",insertable=false,updatable=false, nullable = false)}
)
public ApplicationDevice getApplicationDevice() {
return applicationDevice;
}

第二个实体可能为空,当我尝试存储第一个实体时,我得到一个:

 Cannot add or update a child row: a foreign key constraint fails (`malcom_dev`.`application_device`, CONSTRAINT `FK16A0C0451DC7C799` FOREIGN KEY (`applicationId`, `deviceId`) REFERENCES `APPLICATION_DEVICE_PUSHINFO` (`applicationId`, `deviceId`))

默认情况下可为 true,所以我认为这种关系是可能的。

还有另一种方法可以使实体具有 oneToOne 关系,而无需创建新的列或表,并允许第二个实体可为空?

最佳答案

这不是 Hibernate 而是数据库异常。看起来表上有外键约束。

删除外键约束后它将起作用。

关于java - 可为空到 true 的一对一关系以及使用主键的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445435/

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