简体   繁体   English

如何在同一个表中连接多个列?

[英]How to join multiple column in same table?

I have 2 table "student" and "picklist". 我有2个表“学生”和“选项列表”。
picklist - used it as common table for listing like state, country, city. 选项列表 - 将其用作州,国家,城市等列表的常用表。

Here is student class: 这是学生班:

@Entity
public class Student {
    @Id
    Long id;
    String firstName;
    String lastName;
    Long accountId;

    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumns({@JoinColumn(name="country",referencedColumnName = "name"),
        @JoinColumn(name="accountId",referencedColumnName = "accountId")}
    )
    PickList country;

    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumns({@JoinColumn(name="state",referencedColumnName = "name"),
        @JoinColumn(name="accountId",referencedColumnName = "accountId")}
    )
    PickList state;
}

Here is picklist table: 这是选项列表:

@Entity
@Table(name = "picklist", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"accountId", "name"})
})
public class PickList {
    @Id
    long id;

    long accountId;
    String name;
}

error -- Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: column: accountid (should be mapped with insert="false" update="false") 错误 - Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: column: accountid (should be mapped with insert="false" update="false")

The message is clear: you have a repeated column in the mapping. 消息很明确:映射中有重复的列。 That means that you mapped the same database column twice. 这意味着您将相同的数据库列映射了两次。

Try to do like this @JoinColumn(name="accountId",referencedColumnName = "accountId", insertable = false, updatable = false) 尝试这样做@JoinColumn(name =“accountId”,referencedColumnName =“accountId”,insertable = false,updatable = false)

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

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