This is a followup question to Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?:
Consider the following entity mapping:
public class Zip {
@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code")
private Country country = null
@Column(name = "country_code")
private String countryCode;
...
}
Like this a JPA implementation will choke saying something like "duplicate mapping for country_code, only one of them may be specified as writable".
Q:
Why is making the relationship read-only (private Country country
) preferred here/generally over putting the read-only on the simple ID field (private String countryCode
)?
Why is the one better than the other in practice (pros/cons)?
The only things I can think of is that working with writable relationships is better when the DB is not using any FKs, which would make working with entities somewhat mandatory, however, this would not prevent users from being able to insert new entities into the DB with unknown IDs, which would just make writable relationships slightly safer. Being able to work with the IDs of course would make loading the complete entities from the DB redundant, which can be a big performance aspect.
More arguments welcome.
PS: I added the JPA implementations as tags to attract more audience.