Skip to content
Advertisement

1:n disable constraints for the n-side?

The Problem

I have a 1:n relation, but the n side shouldnt rely on constraints. So i actually wanna insert a EntityPojo via its future id, when its not saved yet ( Lets ignore that its a bad practice ). This looks kinda like this.

JavaScript

Cascading is not possible here, i only have its future ID, not a reference to the object i wanna save. Only its id it will have in the future.

JavaScript

Question

How do we tell hibernate that it should ignore the constraints for this 1:n “target” relation ? It should just insert the given ID into the database, ignoring if that EntityPojo really exists yet.

Glad for any help on this topic, thanks !

Advertisement

Answer

For a much simpler solution, see the EDIT below

If the goal is to insert rows into the join table, without affecting the ENTITY_POJO table, you could model the many-to-many association as an entity itself:

JavaScript

This way, you’ll be able to set a value to the entityId property to a non-existent id, and if an EntityPojo by that id is later inserted, Hibernate will know how to populate relationship properly. The caveat is a more complicated domain model, and the fact that you will need to control the association between RelationshipEntityPojo and EntityPojo using the entityId property, not entity.

EDIT Actually, disregard the above answer, it’s overly complicated. Turing85 is right in that you should simply remove the constraint. You can prevent Hibernate from generating it in the first place using:

JavaScript

The only caveat is that when you try to load RelationshipPojo.targets before inserting the missing EntityPojo, Hibernate will complain about the missing entity, as apparently @NotFound is ignored for @ManyToMany.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement