JPA Association Without Foreign Key
Is it possible to create a ManyToOne association without creating a JPA foreign key in the database?
The tables are owned by a different system and populated asynchronously. So we cannot have FK in the database. There is still, almost always, in the end an attitude.
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="`Order Type`", referencedColumnName = "`Order Type`", insertable = false, updatable = false),
@JoinColumn(name="`Order No`", referencedColumnName = "`No`", insertable = false, updatable = false)
}, foreignKey = @javax.persistence.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private OrderHeader orderHeader;
The problem is that JPA SchemaUpdate is trying to add FK even though ConstraintMode.NO_CONSTRAINT
[error] ohthSchemaUpdate - unable to add foreign key constraint
and we could ignore that if that didn't make the rest of the statements fail
[error] ohthSchemaUpdate - operations are not allowed after the statement is closed.
We are on hibernate-entitymanager 4.3.7.Final and JPA 2.1.
source to share
The fact that ConstraintMode.NO_CONSTRAINT is being ignored looks like a bug in Hibernate 4 due to the fix in 5.
https://hibernate.atlassian.net/browse/HHH-8805
Comments on this and indeed this post are here:
Multiple Relationships with Separate Mapping Table without Hibernate Foreign Key Generation
suggest adding a deprecated annotation (dormant, not JPA)
@ForeignKey( name = "none" )
the attitude should work.
source to share