我有两个实体,分别称为A和B。有一个关系实体C,支持A和B之间的多对多关系。C具有A的外键和B的外键,都标有一个@ManyToOne和@JoinColumn注释。
我的用户希望系统强制执行一个给定的A和B只能创建一个C记录,因此我想声明A外键和B外键的组合密钥必须唯一。
我尝试在C表上使用以下注释,但收到错误消息,列出的外键不存在。
@Table(uniqueConstraints=@UniqueConstraint(name = "UIDX_a_b",columnNames = {"aId,bId"}))
@Entity
@Table(uniqueConstraints=@UniqueConstraint(name = "UIDX_a_b",bId"}))
public class C{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ManyToOne(optional=false)
@JoinColumn(name="aId")
private A a;
@ManyToOne(optional=false)
@JoinColumn(name="bId")
private B b;
...
当我尝试@Table批注时,出现以下错误:
Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (aId,bId) on table C: database column 'aId,bId' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity,especially for relational types)