可选地在Hibernate / JPA中使用级联 [英] Using cascades optionally in Hibernate/JPA
问题描述
简介:
我有一个名为Employee的实体 - 它是我的系统的中心阶层(让我们说它的人力资源管理)。通过写作中心我认为它与其他实体有很多关系。其中有OneToOne,OneToMany和ManyToOne属性。
问题:
在某些情况下,我必须在一个EntityManager上下文中更改此实体(它也具有诸如'String name;'等基本属性) - hibernate Session )并且必须在其他地方刷新。当然,我可以使用它:
entityManager.refreash(employee);
但是...它也会像一百个相关实体一样刷新(提及:1-1 , -1,1- 关系),因为这些关系具有注释参数Cascade.All或Cascade.REFREASH。在这种情况下不需要什么 - 我只在这种情况下使用Employee实体本身。什么是不想要的,因为它会打击性能,甚至在本地需要0.5秒... ...
问题:
有没有什么办法刷新实体可选择关闭级联其属性。或者,也许:有没有什么办法只使用Hibernate / JPA刷新实体的特定属性?
我真的不希望那些20个或更多的SQL查询触发休眠,当我在这些情况下刷新我的实体,但在其他情况下,它是需要的。换句话说,我会说我需要像refreash之前可以设置的respectCascadesOnRefresh = true / false这样的选项。
其他想法?
任何帮助表示赞赏,
首先,如果你只有两个或三个字段可以用来存储一个或多个字段刷新和使用标准API。 其次,Cascade在绝对上下文中不是好东西 - 你最好手动解决一些依赖关系。
第三,为集合使用 FetchType.LAZY
!
PS不,你不能手动使用级联,因为这个注释在生成你的数据库方案时使用过一次。
my problem is quite simple, but very important in general - I think. I have done research for a few hours but didn't find anything useful.
Introduction:
I have entity that is named Employee - which is central class of my system(lets say its HR Management). By writing central I have on my mind that it has many relations to other entites. There are OneToOne, OneToMany and ManyToOne properties in it.
Problem:
In some cases I have to refresh this entity(it has also basic properties like 'String name;' and so on) - after it was changed in one EntityManager context(hibernate Session) and has to be refreshed in other. Of course I can use for it:
entityManager.refreash(employee);
but... it also will refresh like "one hundred" related entites(mentioned: 1-1, -1, 1- relations), because those relations have annotation parameter Cascade.All or Cascade.REFREASH for them. What is not needed in that situation - I only use Employee entity itself in this context. And what is NOT WANTED because it will strike performance and even locally takes like 0.5 s...
Question:
Is there any way to refresh entity optionally "turning off" cascades on its properties. Or maybe: is there any way to refresh only specific properties of entity using Hibernate/JPA??
I really don't want those 20 ore more long SQL queries triggered by Hibernate, when I refresh my entity in those situations, but in other situations it is wanted.
In other words, I would say that I need option like "respectCascadesOnRefresh=true/false" that can be set before refreash.
Any other ideas?
Any help appreciated,
Pedro
Firstly, if you have only two or three fields to refresh, use criteria api.
Secondly, Cascade is not a good thing in absolute context - you'd better resolve some of the dependecies manually.
Thirdly, use
FetchType.LAZY
for collections!
P.S. And NO, you can't use cascade manually, as this annotations are used once while generating your db scheme.
这篇关于可选地在Hibernate / JPA中使用级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!