I have an Entity “A” with a one-to-many relation (1:n) to another Entity “B”. Unfortunately, the (for years existing) database defines the column in B for the primary key of A to NOT NULL, but the relation is not mandatory. Therefore, this column simply gets a -1 if no Emtity A is related.
How can I map this in JPA / Hibernate? I get the following Exception:
Exception in thread "main"
javax.persistence.EntityNotFoundException:
Unable to find A with id -1
I know this is correct, but maybe there’s a workaround?
And please don’t blame me for the data model 🙂
You could use the
@NotFound(action = NotFoundAction.IGNORE)annotation on theB.aproperty. But this would only work when reading the association. If you want to be able to setB.ato null, and make it insert -1 instead of null in database, I have no idea.