如何设置外键可空

8 浏览
0 Comments

如何设置外键可空

我有一个类配置,类似以下内容(只是示例,不是实际意义) -

public class payment
{
    @Id
    @Column("payment_id")
    private int paymentId;
    @ManyToOne
    @JoinColumn(name ="")
    private Fine fine;
    //getter setter和其他内容
}
public class Fine{
    @Id
    @Column("fine_id")
    private int fineId;
    @Column("amount")
    private int fineAmount;
    //其他内容
}

我遇到了`org.hibernate.ObjectNotFoundException: No row with the given identifier exists`的错误信息。根据这个[答案](https://stackoverflow.com/a/8991084/7315895)来说,这是因为外键不能有空值,但我的数据库中包含空值。我不能改变数据库或项目结构,所以有没有办法可以合法地将空值赋给我的外键,即不会产生异常。

0