Well, Let's see
Your exception (and famous) message is
repeated column in mapping for entity:
column: SURVEY_NUM (should be mapped with insert="false" update="false")
Where does SURVEY_NUM column live ?
1º issDivision field stores a foreign key column called SURVEY_NUM
@ManyToOne
@JoinColumns({
@JoinColumn(name="DIVISION_CODE", referencedColumnName="DIVISION_CODE", insertable=false, updatable=false),
@JoinColumn(name="SURVEY_NUM", referencedColumnName="SURVEY_NUM", insertable=false, updatable=false)})
private IssDivision issDivision;
Now, see the following mapping (see both id and accountNumber shares the same column)
@Entity
public class Account {
private Integer id;
private Integer accountNumber;
@Id
@Column(name="ACCOUNT_NUMBER")
public Integer getId() {
return this.id;
}
@Column(name="ACCOUNT_NUMBER")
public Integer getAccountNumber() {
return this.accountNumber;
}
}
Now let's do as follows
Account account = new Account();
account.setId(127359);
account.setAccountNumber(null);
entityManager.persist(account);
Hibernate will ask to you
Which property should i persist whether both properties shares the same column ??? And as i can see, id property stores a non-null value and accountNumber a null value.
Should i perform a query like this one ???
INSERT INTO ACCOUNT (ACCOUNT_NUMBER, ACCOUNT_NUMBER) VALUES (127359, NULL);
It does not make sense. Therefore, it is a wrong SQL query;
Because of that, you see this nice message
repeated column... blah, blah, blah... (should be mapped with insert="false" update="false")
So i suppose your compound primary key called IssTESTPK also stores a column called SURVEY_NUM. And it is not a good idea you define a compound primary key property as insert="false" update="false". Avoid a lot of headache.
Keep in mind: when more than one property shares the same column, define one of them as insertable=false, updatable=false. Nothing else.
I think your compound primary key class should looks like this one
@Embeddable
public class IssTESTPK implements Serializable {
// Ops... Our missing field which causes our Exception (repeated column... blah, blah, blah...)
@Column(name="SURVEY_NUM", nullable=false)
private Integer property;
private Integer otherProperty;
private Integer anotherProperty;
// required no-arg constructor
public IssTESTPK() {}
// You must implement equals and hashcode
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanceof IssTESTPK))
return false;
IssTESTPK other = (IssTESTPK) o;
if(!(getProperty().equals(other.getProperty())))
return false;
if(!(getOtherProperty().equals(other.getOtherProperty())))
return false;
if(!(getAnotherProperty().equals(other.getAnotherProperty())))
return false;
return true;
}
// NetBeans or Eclipse will worry about it
public int hashcode() {
// hashcode code goes here
}
}
UPDATE
Before going on
Hibernate does not support automatic generation of compound primary key
You must provide its values before saving. Keep it in mind
Let's see Employee compound primary key
@Embeddable
public class EmployeeId implements Serializable {
@Column(name="EMPLOYEE_NUMBER")
private String employeeNumber;
@Column(name="SURVEY_NUMBER")
private BigInteger surveyNumber;
// getter's and setter's
// equals and hashcode
}
1º Before saving an Employee, You must provide its values. As said above, Hibernate does not support automatic generation of compound primary key
2º Hibernate does not allow you update a (compound) primary key It does not make sense.
3º Its values can not be null
So, According to described above, our EmployeeId can be written as
@Embeddable
public class EmployeeId implements Serializable {
@Column(name="EMPLOYEE_NUMBER", nullable=false, updatable=false)
private String employeeNumber;
@Column(name="SURVEY_NUMBER", nullable=false, updatable=false)
private BigInteger surveyNumber;
// getter's and setter's
// equals and hashcode
}
As said
when more than one property shares the same column, define one of them as insertable=false, updatable=false. Nothing else
But we can not mark a compound primary key property as insertable=false, updatable=false because of Hibernate uses it to save our Entity
As Hibernate will use our compound primary key property called surveyNumber (and its SURVEY_NUMBER column) to perform SQL operation on database, we need to re-write our @ManyToOne division property (and its foreign key column called SURVEY_NUMBER) as insertable=false, updatable=false
// Employee.java
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="DIVISION_CODE", referencedColumnName="DIVISION_CODE"),
@JoinColumn(name="SURVEY_NUMBER", referencedColumnName="SURVEY_NUMBER", insertable=false, updatable=false)})
private Division division;
4º When you have a compound foreign key, we can not mix insertable-non-insertable or updatable-non-updatable.
Something like
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
// I can be updatable
@JoinColumn(name="DIVISION_CODE", referencedColumnName="DIVISION_CODE", insertable=false),
// And i can be insertable
@JoinColumn(name="SURVEY_NUMBER", referencedColumnName="SURVEY_NUMBER", updatable=false)})
private Division division;
Otherwise, Hibernate will complain
Mixing insertable and non insertable columns in a property is not allowed
Because of that, Its compound foreign key column called DIVISION_CODE should also be marked as insertable=false, updatable=false to avoid the exception shown above
// Employee.java
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="DIVISION_CODE", referencedColumnName="DIVISION_CODE", insertable=false, updatable=false),
@JoinColumn(name="SURVEY_NUMBER", referencedColumnName="SURVEY_NUMBER", insertable=false, updatable=false)})
private Division division;
As we can not update DIVISION_CODE column anymore, our division property behaves like a constant. You, then, think of creating a new property called divisionCode to change, someway, DIVISION_CODE column, as follows
// Employee.java
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="DIVISION_CODE", referencedColumnName="DIVISION_CODE", insertable=false, updatable=false),
@JoinColumn(name="SURVEY_NUMBER", referencedColumnName="SURVEY_NUMBER", insertable=false, updatable=false)})
private Division division;
// Wow, now i expect i can change the value of DIVISION_CODE column
@Column(name="DIVISION_CODE")
private BigInteger divisionCode;
Well, Let's see. Suppose we have the following DIVISION TABLE
DIVISION TABLE
DIVISION_CODE SURVEY_NUMBER
1 10
2 11
3 12
4 13
5 14
remember: There is a foreign key constraint between Division and Employee
You think of
I can not change division property because of insertable=false, updatable=false. But i can change divisionCode property (and its DIVISION_CODE column) as a way to change the foreign key column called DIVISION_CODE
You do the following code
employee.setDivisionCode(7);
Wow, see above in DIVISION_TABLE. Is There some value in DIVISION_CODE column equal to 7 ?
answer is clear: no (You will see a CONSTRAINT VIOLATION)
So, it is an inconsistent mapping. Hibernate does not allow it.
regards,