簡體   English   中英

JPA:如何表達包含@ElementCollection列的唯一約束?

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

這是我的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 ...
}

SearchIndexResponseGroupLocale是枚舉。

此類具有相當復雜的唯一約束: (searchIndex, browseNode, keywords, responseGroups, itemPage, locale)被認為是唯一的。

所以我在類中添加了以下約束注釋:

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

當我嘗試練習課程時,我得到以下異常:

 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

(我使用Hibernate 4.1.7.Final作為JPA實現/服務提供者)

例外要點是: database column responseGroups not found.

如果我沒有在幕后為我的單個實體類指定該約束,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;

我怎樣才能正確表達我的約束?


編輯

這個問題曾經是這樣的:
JPA:Set上的唯一約束(...另一個表上的唯一約束)

進一步思考我的問題,我相信它可以歸結為以下問題:
JPA:如何表達包含@ElementCollection列的唯一約束?

可以使用@CollectionTable和元素集合來定義集合表中列的唯一約束。

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

如上所述,這些是映射元素集合的表的列。 無法對多個表中的列進行唯一約束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM