JPA注解添加唯一约束
程序员文章站
2022-06-12 18:05:41
...
使用JPA注解添加唯一约束
若实体类属性没有指定columnName属性,则coluNames为实体类的属性名称
- 单字段添加唯一约束(未在实体类属性上指定列名称,指定实体约束列为属性名称)
@Table(name = "charge",
uniqueConstraints = {@UniqueConstraint(columnNames="applyId")})
@Column(name = "apply_id",columnDefinition = "Integer(8) COMMENT '申请关联ID'")
private Integer applyId;
- 多字段添加唯一约束(指定列名称)
@Table(name = "project_apply",
uniqueConstraints =
{@UniqueConstraint(columnNames = {"project_id", "customer_id",
"demand_id"})}
/**
*客户信息ID
*/
@Column(name = "customer_id")
private Integer customerId;