简体   繁体   English

JPA:如何表达包含@ElementCollection列的唯一约束?

[英]JPA: How to express a unique constraint including an @ElementCollection column?

This is my JPA entity class: 这是我的JPA实体类:

@Cacheable
@Entity
public class JpaItemSearchRequest implements ItemSearchRequest {

    @Id
    @GeneratedValue
    private long id;

    @Enumerated(EnumType.STRING)
    private SearchIndex searchIndex;

    private String browseNode;
    private String keywords;

    @Enumerated(EnumType.STRING)
    @ElementCollection
    private Set<ResponseGroup> responseGroups = new HashSet<ResponseGroup>();

    private int itemPage;

    @Enumerated(EnumType.STRING)
    private Locale locale;

    ... end of fields ...
}

SearchIndex , ResponseGroup , and Locale are enums. SearchIndexResponseGroupLocale是枚举。

This class has a rather complex unique constraint: a combination of (searchIndex, browseNode, keywords, responseGroups, itemPage, locale) is considered unique. 此类具有相当复杂的唯一约束: (searchIndex, browseNode, keywords, responseGroups, itemPage, locale)被认为是唯一的。

So I added the following constraint annotation to the class: 所以我在类中添加了以下约束注释:

@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "searchIndex", "browseNode", "keywords", "responseGroups", "itemPage", "locale" }) })

When I try to exercise my class, I get the following exception: 当我尝试练习课程时,我得到以下异常:

 org.hibernate.AnnotationException: Unable to create unique key constraint (searchIndex, browseNode, keywords, responseGroups, itemPage, locale) on table JpaItemSearchRequest: database column responseGroups 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)
    at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1584) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1386) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94) ~[hibernate-entitymanager-4.1.7.Final.jar:4.1.7.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ~[hibernate-entitymanager-4.1.7.Final.jar:4.1.7.Final]
    ... 74 common frames omitted

(I use Hibernate 4.1.7.Final as the JPA implementation/service provider) (我使用Hibernate 4.1.7.Final作为JPA实现/服务提供者)

The exception gist is: database column responseGroups not found. 例外要点是: database column responseGroups not found.

If I don't specify that constraint, behind the scenes, for my single entity class, Hibernate generates two tables: One for all the "simple" entity data, and one additional table for the Set<ResponseGroup> responseGroups entity data: 如果我没有在幕后为我的单个实体类指定该约束,Hibernate会生成两个表:一个用于所有“简单”实体数据,另一个表用于Set<ResponseGroup> responseGroups实体数据:

CREATE TABLE `jpaitemsearchrequest` (
    `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
    `browseNode` VARCHAR(255) NULL DEFAULT NULL,
    `itemPage` INT(11) NOT NULL,
    `keywords` VARCHAR(255) NULL DEFAULT NULL,
    `locale` VARCHAR(255) NULL DEFAULT NULL,
    `searchIndex` VARCHAR(255) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;

... ...

CREATE TABLE `jpaitemsearchrequest_responsegroups` (
    `JpaItemSearchRequest_id` BIGINT(20) NOT NULL,
    `responseGroups` VARCHAR(255) NULL DEFAULT NULL,
    INDEX `FKC217757B99AE0422` (`JpaItemSearchRequest_id`),
    CONSTRAINT `FKC217757B99AE0422` FOREIGN KEY (`JpaItemSearchRequest_id`) REFERENCES `jpaitemsearchrequest` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

How can I correctly express my constraint? 我怎样才能正确表达我的约束?


Edit 编辑

This question used to read: 这个问题曾经是这样的:
JPA: Unique constraint across a Set (...unique constraint on another table) JPA:Set上的唯一约束(...另一个表上的唯一约束)

Further thinking about my problem, I'm convinced it can be boiled down to the following question: 进一步思考我的问题,我相信它可以归结为以下问题:
JPA: How to express a unique constraint including an @ElementCollection column? JPA:如何表达包含@ElementCollection列的唯一约束?

Unique constraint over columns in collection table can be defined by using @CollectionTable with element collection. 可以使用@CollectionTable和元素集合来定义集合表中列的唯一约束。

@CollectionTable(
    uniqueConstraints= @UniqueConstraint(columnNames={"col1","col2"})
)

As said, those are columns of table where element collection is mapped. 如上所述,这些是映射元素集合的表的列。 There is no way to have unique constraint over columns in multiple tables. 无法对多个表中的列进行唯一约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM