JPA协会没有外键 [英] JPA association without foreign key

查看:315
本文介绍了JPA协会没有外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在不使用JPA的情况下创建一个ManyToOne关联,并在数据库中创建一个外键?



这些表由另一个系统拥有,并被异步填充。因此我们在数据库中不能有FK。几乎总是有一个关系。

  @ManyToOne(fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name =`Order Type`,referencedColumnName =`Order Type`,insertable = false,updatable = false),
@JoinColumn(name =`Order No` ,referenceColumnName =`No`,insertable = false,updatable = false)
},foreignKey = @ javax.persistence.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
Private OrderHeader orderHeader;

问题在于JPA SchemaUpdate尝试添加FK,即使ConstraintMode.NO_CONSTRAINT



[错误] ohthSchemaUpdate - 无法添加外键约束


我们可以忽略它,如果它没有使其余的语句失败



[error] ohthSchemaUpdate - 语句关闭后不允许任何操作。


我们在hibernate-entitymanager 4.3.7.Final和JPA 2.1中。

解决方案

忽略ConstraintMode.NO_CONSTRAINT,看起来像是Hibernate 4中的一个bug,因为它被固定在5中。



https://hibernate.atlassian.net/browse/HHH-8805



一个d确实这篇文章在这里:

与单个映射表的多个关系,而不用Hibernate生成外键



建议添加已弃用的(hibernate而不是JPA) annotation

  @ForeignKey(name =none)

到关系应该有效。


Is it possible to set up a ManyToOne association without JPA creating a foreign key in the database?

The tables are owned by another system and are populated asynchronously. Thus we can't have a FK in the database. There's still, almost always, eventually a relation.

@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 tries to add a FK even though ConstraintMode.NO_CONSTRAINT

[error] o.h.t.h.SchemaUpdate - Cannot add foreign key constraint

and we could ignore that if it didn't make the rest of the statements fail

[error] o.h.t.h.SchemaUpdate - No operations allowed after statement closed.

We're on hibernate-entitymanager 4.3.7.Final and JPA 2.1.

解决方案

The fact that ConstraintMode.NO_CONSTRAINT is ignored looks like a bug in Hibernate 4 due to be fixed in 5.

https://hibernate.atlassian.net/browse/HHH-8805

The comments on that and indeed this post here:

Multiple relationships with single mapping table without generating foreign keys by Hibernate

suggest adding the deprecated (hibernate rather than JPA) annotation

@ForeignKey( name = "none" )

to the relationship should work.

这篇关于JPA协会没有外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆