Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

Kawu :

When implementing composite primary keys in Hibernate or other ORMs there are up to three places where to put the insertable = false, updatable = false in composite primary key constellations that use identifying relationships (FKs that are part of the PK):

  1. Into the composite PK class' @Column annotation (@Embeddable classes only) or
  2. Into the entity class' association @JoinColumn/s annotation or
  3. Into the entity class' redundant PK property's @Column annotation (@IdClass classes only)

The third is the only way to do with @IdClass and JPA 1.0 AFAIK. See http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships. I will consider only cases 1. and 2.

Q: Which way is the preferred place to put the "insertable = false, updatable = false" to generally?

I have experienced problems with Hibernate concerning this question. For example, Hibernate 3.5.x will complain about the Zips table

CREATE TABLE Zips
(
  country_code CHAR(2),
  code VARCHAR(10),
  PRIMARY KEY (country_code, code),
  FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)

with:

org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...

As you can see the country_code column is both PK and FK. Here are its classes:

Entity class:

@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
    @EmbeddedId
    private ZipId id;

    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code")
    private Country country = null;
...
}

Composite PK class:

@Embeddable
public class ZipId implements Serializable
{
    @Column(name = "country_code", insertable = false, updatable = false)
    private String countryCode;

    @Column(name = "code")
    private String code;
...
}

When putting the insertable = false, updatable = false into the entity class association's @JoinColumn all exceptions disappear and everything work fine. However, I don't see why the above code should not be working. It might be Hibernate having problems with this. Is the described a Hibernate bug, as it doesn't seem to evaluate @Column "insertable = false, updatable = false"?

In essence, what's the standard JPA way, the best practice, or preference where to put "insertable = false, updatable = false"?

ManuPK :

Let me answer step by step.

1. When do you need ` insertable = false, updatable = false`?

Lets look at the below mapping,

public class Zip {

    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code")
    private Country country = null

    @Column(name = "country_code")
    private String countryCode;

}

Here we are referring to the same column in the table using two different properties. In the below code,

Zip z = new Zip();

z.setCountry(getCountry("US"));
z.setCountryCode("IN");

saveZip(z);

What will hibernate do here??

To prevent these kind of inconsistency, hibernate is asking you to specify the update point of relation ships. Which means you can refer to the same column in the table n number of times but only one of them can be used to update and all others will be read only.

2. Why is hibernate complaining about your mapping?

In your Zip class you are referring to the Embedded id class ZipId that again contains the country code. As in the above scenario now you have a possibility of updating the counry_code column from two places. Hence error given by hibernate is proper.

3. How to fix it in your case?

No. Ideally you want your ZipId class to generate the id, so you should not add insertable = false, updatable = false to the countryCode inside the ZipId. So the fix is as below modify the country mapping in your Zip class as below,

@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code",
insertable =  false, updatable = false)
private Country country;

Hope this helps your understanding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Hibernate Mapping composite primary key that contains foreign keys

Do CascadeType.ALL and "insertable = false, updatable = false" exclude each other?

JPA / Hibernate - Composite primary key with foreign key

Hibernate foreign key with a part of composite primary key

JPA & Hibernate - Composite primary key with foreign key

JPA & Hibernate - Composite primary key with foreign key

Foreign key relationship with composite primary keys in mysql

Sequelize, foreign keys as composite primary key

Spring save composite primary key with foreign keys

Composite primary key of two foreign keys

How do I properly map entities where a primary key is composed of two foreign keys, one of which is composite itself?

hibernate composite Primary key contains a composite foreign key, how to map this

Mapping composite foreign key to composite primary key where the foreign key is also a primary key

Do i need index for composite primary key where each column is foreign key

Entity mapping for one to many with multiple foreign keys as composite primary key

Entity Framework - Composite Primary Key formed by 3 Foreign Keys

Is possible to have a Composite Primary Key which components are entity foreign keys?

Hibernate Composite Primary Key

Composite foreign key as primary key

JPA Composite Foreign Primary keys

Mapping a composite foreign key to a composite primary key

Please explain about insertable=false and updatable=false in reference to the JPA @Column annotation

Hibernate updatable = false UUID field is updated

Hibernate @OneToOne with updatable = false still updating

Read data from table where primary key is composite using hibernate

How to use two foreign keys as primary key on Hibernate entity annotation

EntityFramework - Composite keys and foreign key

JPA composite primary key/foreign key mapping

Sequelize foreign key reference composite primary key