我有多个 PK:我的表的主键是 (idQuestion, id_parent)。
在 QuestionsId 类中,我已经:
@Embeddable
public class QuestionsId implements Serializable
{
@Column(name = "id_parent", nullable = true)
private Integer id_parent;
@Column(name = "idQuestion", nullable = false)
private Integer idQuestion;
//Setters, Getters, etc
}
在问题类中:
@EmbeddedId
@Column(nullable = true, unique = true)
private QuestionsId idQuestion;
//Setters, Getters, etc
id_parent 应该包含 null 值 我尝试输入 nullable=true 但它不起作用!我怎样才能指定这个PK可以有一个空值!?
最佳答案
如果它可以为空,那么它不应该是主键。主键是唯一标识符,如果其复合属性之一可能为空,则有可能违反唯一性。
关于java - Spring jpa中如何指定主键可以为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45411841/