JPA单向@OneToMany性能 [英] JPA unidirectional @OneToMany performance

查看:132
本文介绍了JPA单向@OneToMany性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体单向@OneToMany Lazy 关系。当我尝试添加一个孩子时,看起来像Hibernate 4(我的JPA提供者)实际执行


  1. 选择查询

  2. 在连接表上删除所有具有该父母标识的孩子

  3. 重新插入连接表上的所有孩子和新孩子

如何让Hibernate在不改变我的关系的情况下插入我希望的子项?

解决方案

默认情况下,单向 @OneToMany 关系将使用连接表,它将按照我的问题执行操作。如果您使用的是JPA2并且不对父节点使用多态,则可以添加 @JoinColumn ,这将在children表上创建外键而不是另一个连接表。 JPA提供程序然后将不执行删除并重新插入。

I have two entities with unidirectional @OneToMany Lazy relationship. When I try to add a child, it seems like Hibernate 4 (my JPA provider) actually performs

  1. Select query
  2. Delete all children with that parent id on join table
  3. Reinsert back all and the new child on join table

How to make Hibernate to just insert the child I wish, without changing my relationship?

解决方案

By default, a unidirectional @OneToMany relationship will use a join table, it will perform operation as my question. If you are using JPA2 and do not use polymorphic on parent, you may add @JoinColumn, which will create foreign key on children table instead of another join table. JPA provider then will not perform delete and reinsert again.

这篇关于JPA单向@OneToMany性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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