Using Java Hibernate, how can you use nullable composite foreign keys when part of the composite key is part of your primary key?

Specifically with our database relationship being like so, title:

Table S

column id
...other irrelevant columns omitted...
primary key on id only

Table C

column S_id
column id
...
compound primary key on S_id (which is a foreign key to id column in table S) and id.

Table I

column S_id
column id
column C_id
...
compound primary key again on S_id and id.
compound foreign key on C_id and S_id, but C_id column in this table is also nullable.

I have attempted the following for java hibernate annotations on IObject for modeling table I:

@ManyToOne
@JoinColumns({
    @JoinColumn({name="C_id", referencedColumnName="id",insertable=false,updateable=false),
    @JoinCOlumn({name="S_id", referencedColumnName="S_id", insertable=false,updateable=false)
})
private CObject _cObjectRef;
 
@EmbeddedId
private EmbeddedIdCompositeKeyObject _primaryKey;
...

Here is the important parts of java hibernate annotations on CObject for modeling table C:

@EmbeddedId
private EmbeddedIdCompositeKeyObject _primaryKey;
 
@MapsId("_s_id")
@ManyToOne(optional=false)
@JoinColumn(name="S_id", nullable = false)
private SObject _sObject;
...

And here is what EmbeddedIdCompositeKeyObject looks like (its @Embeddable and implements Serializable):

@Column(name = "id", nullable = false)
private int _id;
@Column(name = "S_id", nullable = false)
private int _s_id;

...
constructors, getters, hashCode, and equals ommitted.

However, when run, hibernate fails to update these columns of table I despite the _cObject being set to an existing CObject instance that matches what exists in the database. Every other column in the I object table saves and updates correctly upon calling hibernate's session.saveOrUpdate method on IObject except one where this nullable compound foreign key is used.

I have tried removing insertable and updateable qualifiers on the JoinColumns, or adding them in different combinations of true or false, and even mixing nullable in, however when that is attempted errors such as the following appear:

  1. Mixing nullable and non nullable columns in a property is not allowed (this seems like what I need though)

  2. Mixing updatable and non updatable columns in a property is not allowed

  3. Mixing insertable and non insertable columns in a property is not allowed

  4. Repeated column in mapping for entity: foo.bar.IObject columns S_id (should be mapped with insert="false" update="false"

Adding @MapsId("_s_id") to IObject's CObject reference doesn't appear to make a difference.

This question is somewhat similar to another and almost exactly like another, however neither questions appear to ask about cases with nullable composite foreign keys with primary key overlap, and none of the proposed solutions appear to work for this case, either.



Read more here: https://stackoverflow.com/questions/65724388/using-java-hibernate-how-can-you-use-nullable-composite-foreign-keys-when-part

Content Attribution

This content was originally published by schnejj at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: