I have an entity object and i want to add a constraint on 2 columns.
I have 2 columns that should be unique:
- resource_name : string (non null)
- entity_id : foreign key (nullable)
I want to be able to create a resource in db with same resource name but this value must be unique depending on entity id. I added the follow annotation
@Entity
@Table(
name = "resources",
uniqueConstraints = {
// note! on a une contrainte sur le resourceName et entityId
@UniqueConstraint(
name = "uniqueEntityAndResourceNameConstraint",
columnNames = { "entity_id", "resource_name" }
)
}
)
The issue is that the constraint do not allow me to persist resource with same resource name when entity id is null… Is there a solution for this ?