开发者

Using cascades optionally in Hibernate/JPA

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜