JPA:如何表达包含@ElementCollection列的唯一约束? [英] JPA: How to express a unique constraint including an @ElementCollection column?

查看:1025
本文介绍了JPA:如何表达包含@ElementCollection列的唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的JPA实体类:

  @Cacheable 
@Entity
public class JpaItemSearchRequest implements ItemSearchRequest {

@Id
@GeneratedValue
私人长ID;

@Enumerated(EnumType.STRING)
私有SearchIndex searchIndex;

私人字符串browseNode;
私人字符串关键字;

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

private int itemPage;

@Enumerated(EnumType.STRING)
私有语言区域设置;

...字段结尾...
}

SearchIndex ResponseGroup Locale 是枚举。 / b>

这个类有一个相当复杂的唯一约束:
(searchIndex,browseNode,keywords,responseGroups,itemPage,locale)的组合被认为是唯一的。



因此,我将以下约束注释添加到类中:

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

当我尝试锻炼我的课程时,出现以下异常:

  org.hibernate.AnnotationException:无法在表JpaItemSearchRequest上创建唯一键约束(searchIndex,browseNode,keywords,responseGroups,itemPage,locale):数据库列responseGroups not found 。确保使用正确的列名,这取决于所使用的命名策略(它可能与实体中的属性名称不同,特别是关系类型)$ or $ $ b $ 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]
在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个常用框架省略

(我使用 Hibern 4.1.1.Final 作为JPA实现/服务提供者)

异常要点是:未找到数据库列responseGroups。



如果我没有在后台为我的单个实体类指定该约束,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 ;

如何正确表达我的约束?



< hr>

编辑



此问题用于阅读:

JPA:一个Set(...在另一个表上的唯一约束)



进一步思考我的问题,我相信它可以归结为以下问题:

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

解决方案

集合表中的列的唯一约束可以使用 @CollectionTable 与元素集合。

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

如上所述,表元素集合映射的表。没有办法对多个表中的列进行唯一约束。


This is my JPA entity class:

@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.

This class has a rather complex unique constraint: a combination of (searchIndex, browseNode, keywords, responseGroups, itemPage, locale) is considered unique.

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)

The exception gist is: 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:

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)

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?

解决方案

Unique constraint over columns in collection table can be defined by using @CollectionTable with element collection.

@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.

这篇关于JPA:如何表达包含@ElementCollection列的唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆