2

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.

cventr
  • 186
  • 1
  • 12

1 Answers1

0

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"

Alexander Petrov
  • 9,204
  • 31
  • 70
  • I've already tried your solution but without any result. After googling I've found a second way to define a composite key: with `@IdClass`. I still don't know what are the main differences between `@IdClass` and `@Embeddable` but for some reason with the first one everything is working properly. – cventr Jul 24 '16 at 14:20
  • Using IdClass and Embedded Id may be interchangeable. The EmbededId is a little bit more flexible compared to "@idClass" essentially it is the same. Please read here to see the differences. Both way is good with "@EmbededId" being the more flexible approach http://stackoverflow.com/questions/212350/which-annotation-should-i-use-idclass-or-embeddedid – Alexander Petrov Jul 24 '16 at 14:25
  • The key moment in my post is that you can have only one updatable insertable row and everything else needs to have update=fale insert=false – Alexander Petrov Jul 24 '16 at 14:28