I solved this problem: the link to the parent cannot be insertable or updatable, because the column TABLE1_CODE is part of this link and this column is also part of the primary key. To be able to give a value to the column PARENT_CODE, I added the property parentCode (which is insertable and updatable). I do not have a better solution. If anyone has a better one, I take it. ;o)
Here is the code of the Table2 class:
Code:
package example;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "TABLE2")
public class Table2 {
private PK id;
@EmbeddedId
public PK getId() {
return id;
}
public void setId(PK id) {
this.id = id;
}
private String parentCode;
@Column(name = "PARENT_CODE")
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
private Table2 parent;
@JoinColumns({
@JoinColumn(name = "TABLE1_CODE", referencedColumnName = "TABLE1_CODE", insertable = false, updatable = false),
@JoinColumn(name = "PARENT_CODE", referencedColumnName = "CODE", insertable = false, updatable = false)})
@ManyToOne(fetch = FetchType.LAZY)
public Table2 getParent() {
return parent;
}
public void setParent(Table2 parent) {
this.parent = parent;
}
@Embeddable
public static class PK implements Serializable {
private Table1 table1;
@ManyToOne
@JoinColumn(name = "TABLE1_CODE", updatable = false)
public Table1 getTable1() {
return table1;
}
public void setTable1(Table1 table1) {
this.table1 = table1;
}
@Column(name = "CODE", updatable = false)
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
}