Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在JPA中对外键设置唯一约束_Hibernate_Jpa_Foreign Keys_Unique Constraint - Fatal编程技术网

Hibernate 如何在JPA中对外键设置唯一约束

Hibernate 如何在JPA中对外键设置唯一约束,hibernate,jpa,foreign-keys,unique-constraint,Hibernate,Jpa,Foreign Keys,Unique Constraint,使用JPA(hibernate,spring工具套件),我试图对两个表列进行唯一性约束,其中一个是外键。我搞不懂语法 我试过了 @Table(uniqueConstraints={@UniqueConstraint(columnNames = {"company.companyId" , "state"})}) @Entity public class Branch { @Id @GeneratedValue(strategy=GenerationType.AUTO) p

使用JPA(hibernate,spring工具套件),我试图对两个表列进行唯一性约束,其中一个是外键。我搞不懂语法

我试过了

@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"company.companyId" , "state"})})
@Entity
public class Branch {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int branchId;
    @ManyToOne(optional = false)
    private Company company;
    @Column
    int state;
}
但我得到了一个错误:

database column 'company.companyId' 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)
所以我尝试了其他几种方法,比如“公司id”、“公司id”和“公司id”。它们都不起作用。

我试过“公司id”,现在它起作用了


很难注意到约束的存在。我使用的是MSSM,它不会显示在“约束”文件夹中。为了正确地查看约束,您必须“为要创建的表编写脚本”。
@UniqueConstraint
列名
总是指物理表列,而不是实体关系/属性
company\u company\u id
可能是JPA/Hibernate为您创建公司表时生成的默认名称,除非您通过
@column
明确指定了列名annotation@lsh谢谢你澄清这一点。我做的是“代码优先”,我没有给我的任何列命名(因此使用默认名称),这可能不是最好的选择。