How to join multiple columns in one table?

I have 2 tables "student" and "picklist".
picklist - used it as a general table for listing, as a state, country, city.

Here is the 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 the selection 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")

+4
source share
1 answer

The message is clear: you have a repeated column in the mapping. This means that you have mapped one database column twice.

Try this @JoinColumn (name = "accountId", referenceColumnName = "accountId", insertable = false, updatable = false)

0
source

Source: https://habr.com/ru/post/1652756/


All Articles