jpa中的Nullable Map映射

问题描述 投票:1回答:1

我正在尝试使用JPA2.1和Hibernate 4.3.7将实体键与实体值映射映射到数据库。这是我的代码:

@Entity
@Audited
class Form{

    //id

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "form_content",
        joinColumns = @JoinColumn(name = "id_form", nullable = false),
        inverseJoinColumns = @JoinColumn(name = "id_field_content"/*non-working part start*/, unique = false, nullable = true/*non-working part stop*/),
        uniqueConstraints = { @UniqueConstraint(columnNames = { "id_field", "id_form" }) })
    @MapKeyJoinColumn(name = "id_field", nullable = false)
    private Map<FormEntryDefinition, FormEntryValue> content;

    //getters, setters, equals, etc.
}

Hibernate生成表

form_content(
    id_form bigint primary key not null,
    id_field_content bigint <!-- problem start -->not null unique <!--problem stop -->,
    id_field bigint primary key not null)

在所有3个字段中使用适当的外键。

任何人都可以告诉我,为什么hibernate生成唯一的而不是null的约束,所以我不能坚持使用可空值的map?

该问题有解决方法吗?

java hibernate jpa orm hibernate-mapping
1个回答
1
投票

唯一约束是由于使用@OneToMany关联。如果将其更改为@ManyToMany,则不再需要唯一约束。

连接表FK列仅对非可空列有意义。连接表中的一行是两个表之间的链接,如果缺少一个FK,则无论如何关联都会中断,这相当于首先没有链接行。

© www.soinside.com 2019 - 2024. All rights reserved.