Home > OS >  Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:

Time:04-21

I have 2 tables with One to Many Relationship and one table has composite primary key.

Table 1: Key_Numbers – Columns - ID_UNIK( Primary Key) and other columns Table2: Key_Number_xml – Columns - ID_UNIK_Key_Number( Primary key as well Foreign key mapped with ID_UNIK from Key_Numbers table), Line_Number (Another primary Key), Data, So, we have composite primary key and one of the them is Foreign Key

I have 3 Java classes –

  • Keynumber - Entity Class for Key_Numbers table
  • keynumberXMLBeanKey for composite primary Keys
  • keynumberXMLBean for child class

I am getting the below error message in Jboss EAP application server:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: keynumberXMLBean column: ID_UNIK_Key_Number (should be mapped with insert="false" update="false")"}} }

I am not sure should we map ID_UNIK_Key_Number in both classes (Key class and Main class) or not.

@Entity
@Table(name = "Key_Numbers")
public class Keynumber implements Serializable {

    private static final long serialVersionUID = 2816559889798956666L;

    @Id
    @GeneratedValue
    @Column(name = "ID_UNIK")
    private Integer id_unik;

    @OneToMany(mappedBy = "keynumber")
    private Set<keynumberXMLBean> keynumberXMLBeans = new HashSet<keynumberXMLBean>(0);


@Embeddable
public class keynumberXMLBeanKey implements java.io.Serializable {

    static final long serialVersionUID = 3206093459760846163L;

    @Column(name = "Line_Number")
    public Integer linenumber;

    @Column(name = "ID_UNIK_Key_Number")
    public Integer idUnikKeyNumber;

@Entity
@Table(name = "Key_Number_xml")
public class keynumberXMLBean {

    @EmbeddedId
    private keynumberXMLBeanKey pk;

    @Column(name = "DATA")
    private String data;

    @ManyToOne
    @JoinColumn(name = "ID_UNIK_Key_Number")
    private Keynumber keynumber;

CodePudding user response:

As the error states, you have the "ID_UNIK_Key_Number" column mapped twice in the keynumberXMLBean entity, once within the keynumber ManyToOne, and a second time within the keynumberXMLBeanKey embeddable as a basic idUnikKeyNumber property. This causes problems as you've specified both as controlling that column, so it cannot know which to use to set the value if they conflict. The solution, as the error states, is to mark one as read only (insertable=false, updatable=false)

@Entity
@Table(name = "Key_Number_xml")
public class keynumberXMLBean {

    @EmbeddedId
    private keynumberXMLBeanKey pk;

    @Column(name = "DATA")
    private String data;

    @ManyToOne
    @JoinColumn(name = "ID_UNIK_Key_Number", insertable=false, updatable=false)
    private Keynumber keynumber;
}

Doing this means JPA will pretty much ignore the keynumber reference on write operations, instead populating the FK column using the keynumberXMLBean.pk.idUnikKeyNumber - so your application is required to set it from the referenced keynumber.id_unik value. This of course might be a problem if you are creating both in the same transaction and why JPA has derived IDs, allowing you to mark your relationship as part of the ID without mapping the foreign key as a basic column, or have both with @MapsId annotation.

Assuming you still want an encapsulated embeddedID with the value:

@Entity
@Table(name = "Key_Number_xml")
public class keynumberXMLBean {

    @EmbeddedId
    private keynumberXMLBeanKey pk;

    @Column(name = "DATA")
    private String data;

    @MapsId(value = "idUnikKeyNumber")
    @ManyToOne
    @JoinColumn(name = "ID_UNIK_Key_Number")
    private Keynumber keynumber;
}

With this, JPA will use the relationship to set the keynumberXMLBean.pk.idUnikKeyNumber value (and so the fk) for you from the generated sequence within Keynumber when it needs to (post persist/flush/commit - when ever it would generate the sequence value).

  • Related