I want to create a foreign key that maps an object by someId
in this example:
@Entity
public class MyEntity {
private int someId;
@ManyToOne
@JoinColumn(name = "someId", foreignKey = @ForeignKey(name="fk_id"), insertable = false, updatable = false, nullable = true)
private MyObject obj;
}
As you see: someId
creates also a foreign key reference to MyObject
by the same id.
Problem: I want to be able to set someId
, and at the same time a MyObject
in table my_object_table
may not exist.
Is that possible at all?
I someId
is set but the same id does not exist in the foreign table,I'm getting the following error on persistence:
Caused by: org.postgresql.util.PSQLException: ERROR: Insert or Update in table "my_entity" violates forgein key constraint "fk_id".
Detail: Key (some_id)=(940) is not contained within table "my_object_table".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)