I have a couple of tables with relation as in the image below
I created hibernate data model as follows
@Entity
@Table(name = "SUBJECT")
public class Subject {
@Column(name = "NAME")
private String name;
@Column(name = "ADDRESS")
private String address;
@Column(name = "CLIENT_ID")
private String clientId;
@OneToMany(mappedBy = "subject", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<SSI> SSIs;
// getters and setters
...
}
@Entity
@Table(name = "SUBJECT_IDENTIFIER")
public class SubjectIdentifier {
@Column(name = "VALUE")
private String value;
@Column(name = "AUTHORITY")
private String authority;
@Column(name = "TYPE")
private String type;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID", insertable = true,
updatable = true,
@JoinColumn(name = "CLIENT_ID", referencedColumnName = "CLIENT_ID", insertable =
true, updatable = true)
})
private Subject subject;
// getters and setters
...
}
@Entity
@Table(name = "SSI")
public class SSI {
@ManyToOne
@JoinColumns({
@JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID", insertable = true,
updatable = true),
@JoinColumn(name = "CLIENT_ID", referencedColumnName = "CLIENT_ID", insertable =
true, updatable = true)
})
private Subject subject;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "SUBJECT_IDENTIFIER_ID", referencedColumnName = "ID", insertable = true,
updatable = true),
@JoinColumn(name = "CLIENT_ID", referencedColumnName = "CLIENT_ID", insertable =
true, updatable = true)
})
private SubjectIdentifier subjectIdentifier;
// getters and setters
...
}
I intend to create the entities as follows
...
Subject s = new Subject();
//.. initialization of s goes here
SubjectIdentifier si = new SubjectIdentifier();
//.. initialization of si goes here
SSI ssi = new SSI();
ssi.setSubject(s);
ssi.setSubjectIdentifier(si);
s.setSSI(ssi);
...
emProvider.get().persist(s);
When I run this, I get following error
org.hibernate.MappingException: Repeated column in mapping for entity: *.SSI column: CLIENT_ID (should be mapped with insert="false" update="false")
If I set insert="false" update="false" for CLIENT_ID, it would error again about mixing of insert & update with other column in the @Joincolumns
If I set insert="false" update="false" for all the @JoinColumns then it will not persist the objects.
How to really handle this kind of entity creation?

That's not so easy. If you want that, you have to introduce another attribute for storing the client id and maintain this denormalization: