当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Hibernate: Repeated column in mapping for entity

问题描述:

I've found a lot of questions about this but they did not solve my problem.

This is the structure that I'm trying to map:

TABLE A

col0 (PK) | colA (PK) | colB

TABLE B

col0 (PK) |colA (PK) | colC (PK) | ColD

TABLE C

col0 (PK) | colA (PK) | colC (PK) | ColE (PK) | ColF

I have a problem on the third table (Table C). The exception thrown is

org.hibernate.MappingException: Repeated column in mapping for entity: TableC column: colA (should be mapped with insert="false" update="false")

Table C:

@Entity

@Table(name="TableC")

public class TableC implements Serializable{

@EmbeddedId

private TableCId tableCId;

@MapsId("tableBId")

@ManyToOne

@JoinColumns({

@JoinColumn(name="col0", referencedColumnName="col0"),

@JoinColumn(name="colC", referencedColumnName="colC"),

@JoinColumn(name="colA", referencedColumnName="colA")

})

private TableB tableB;

// Getters and setters

}

Here I defined the TableCId

 @Embeddable

public static class TableCId implements Serializable{

private TableBId tableBId;

private Integer colE;

// Getters and setters

// equals and hashcode

}

And then the TableBId

@Embeddable

public static class TableBId implements Serializable{

private Integer col0;

private Integer colA;

private Integer colC;

// Getters and setters

// equals and hashcode

}

I already tried to put insertable=false, updatable=false in @JoinColumn but with no result.

网友答案:

Well it is not very surprising you have once defined the ColumnA as part of your @EmbededID and second time you are mapping it as part of your @JoinColumn in private TableB tableB;

On your @ManyToOne mapping in the @JoinColumn you can try to set insertable=false, updateable=false

Then potentially it may work.

Let me clarify a bit more TableC has EmbededId TableCId which references TableBId which is @Embedable. This means that everything mapped in TableBid will be also mapped in the TableC. Accidentaly it happens that this is "colA" which also happens to be mapped second time through the @JoinColumn in the @ManyToOne mapping to TableB in TableC. At the end you are mapping "colA" twice.

Since your first mapping is part of the ID which means that you need to be able to insert it. The only way to fix this situation is to mark the second mapping that is through the @ManyToOne TableB to "insertable=false" updateable="false"

分享给朋友:
您可能感兴趣的文章:
随机阅读: