JPA:固定值连接元素集合中的列

时间:2017-12-06 10:14:40

标签: java hibernate jpa

我想通过在每个连接中使用固定标识符来减少数据库中类似表的总数。

目前我的实体类看起来像这样:

public class Group {

    [...]

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(
            name = "group_admins",
            joinColumns = @JoinColumn(name = "group_id"))
    @Column(name = "name", unique = true, nullable = false)
    @OrderBy
    private Set<String> admins = new LinkedHashSet<>();

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(
            name = "group_moderators",
            joinColumns = @JoinColumn(name = "group_id"))
    @Column(name = "name", unique = true, nullable = false)
    @OrderBy
    private Set<String> moderators = new LinkedHashSet<>();     

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(
            name = "group_user",
            joinColumns = @JoinColumn(name = "group_id"))
    @Column(name = "name", unique = true, nullable = false)
    @OrderBy
    private Set<String> users = new LinkedHashSet<>();

    [...]

}

这导致它创建1 + 3 = 4个表。其中3个共享完全相同的列。现在我想把它减少到1 + 1 = 2个表格,如:https://docs.oracle.com/html/E13946_05/ref_guide_mapping_notes_nonstdjoins.html

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(
            name = "person",
            joinColumns = {
                    @JoinColumn(name = "group_id", referencedColumnName="id"),
                    @JoinColumn(name = "person.type", referencedColumnName="'user'")
            })
    @Column(name = "name", unique = true, nullable = false)
    @OrderBy
    private Set<String> users = new LinkedHashSet<>();

我希望创建一个这样的表:

    • group_id:int
    • type:varchar [&#39; user&#39;,&#39;主持人&#39;,&#39; admin&#39;]
    • name:varchar

但我总是得到一个类似于这个的例外:

Caused by: org.hibernate.MappingException: Unable to find column with logical name: 'user' in group
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:858) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:243) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1611) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1352) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:810) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:735) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]

它甚至没有尝试执行SQL。

我也试过@DescriminatorColumn,但这甚至不适用于属性。

有没有办法在不使用额外实体/嵌入的情况下做到这一点?

我用

  • Java 8
  • Hibernate 5.2.12
  • Spring Boot

0 个答案:

没有答案