0

我有两个具有 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`))

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

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

4

1 回答 1

1

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

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

于 2013-07-03T10:25:10.960 回答