我想创建一个具有自动生成主键的实体,但也是一个由两个其他字段组成的唯一复合键.我如何在JPA中执行此操作?
我想这样做是因为主键应该在另一个表中用作外键并使其复合并不好.
在下面的代码片段中,我需要命令和模型是唯一的.pk当然是主键.
@Entity @Table(name = "dm_action_plan") public class ActionPlan { @Id private int pk; @Column(name = "command", nullable = false) private String command; @Column(name = "model", nullable = false) String model; }
Michel.. 18
你可以使用@UniqueConstraint
这样的东西:
@Entity @Table(name = "dm_action_plan", uniqueConstraints={ @UniqueConstraint(columnNames= "command","model") } ) public class ActionPlan { @Id private int pk; @Column(name = "command", nullable = false) private String command; @Column(name = "model", nullable = false) String model; }
这将允许您的JPA实现为唯一约束生成DDL.
你可以使用@UniqueConstraint
这样的东西:
@Entity @Table(name = "dm_action_plan", uniqueConstraints={ @UniqueConstraint(columnNames= "command","model") } ) public class ActionPlan { @Id private int pk; @Column(name = "command", nullable = false) private String command; @Column(name = "model", nullable = false) String model; }
这将允许您的JPA实现为唯一约束生成DDL.