I got these 2 entities:
@javax.persistence.Entity
public class Book {
@javax.persistence.EmbeddedId
private BookPK id;
private String title;
@javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
@javax.persistence.JoinColumns({
@javax.persistence.JoinColumn(name = "LNGCOD", referencedColumnName = "LNGCOD"),
@javax.persistence.JoinColumn(name = "LIBCOD", referencedColumnName = "LIBCOD") })
private Language language;
}
@javax.persistence.Entity
public class Language {
@javax.persistence.EmbeddedId
private LanguagePK id;
private String name;
}
with composed PK’s:
@Embeddable
public class BookPK implements Serializable {
private Integer bookcod;
private Integer libcod;
}
@Embeddable
public class LanguagePK implements Serializable {
private Integer lngcod;
private Integer libcod;
}
If I try to create a new Book and persist it, I get an exception telling me libcod is found twice in the insert statement (“Column ‘libcod’ specified twice”). But I can’t use “insertable = false” when defining the JoinColumn (“Mixing insertable and non insertable columns in a property is not allowed”).
Is there any way to define these objects + relationship so the columns are managed automatically by Hibernate ?
Hibernate and JPA automatically make persistent all the modifications made to persistent entities while they are attached to the session. That’s the whole point of an ORM: you load a persistent object, modify it, and the new state is automatically persisted at the commit of the transaction, without any need to call persist, merge, save or any other method.
Note that calling
persiston a persistent entities (except for its cascading side-effects) makes no sense.persistis to make a transient entity (i.e. a new one, not in the database yet, with no generated ID) persistent.