Dedicated cache region for entity subclasses?

≯℡__Kan透↙ 提交于 2019-12-04 03:11:00

Hibernate caches the entire entity hierarchy in one region explicitly. There is no control over this. Its the only way to properly handled cached resolution of polymorphic lookups. Other providers (I think) do allow some control over where/how each subclass is cached (at least thats my guess based on the options provided by JPA).

I have had this issue, and searching for internet I have ending in this Stackoverflow thread. It is really instructive to me due to I have the same issue that the one explained in this question. Reading the discussion between Steve Ebersole and Jan Goyvaerts address me to the solution of my case. Specially the link provided by one of them to Hibernate forum.

Testing the recommendations of the forum (also has passed three years from this question, then probably something has changed) I have success in generating different cached regions for different children of a parent class.

In my case, the solution was using @MappedSuperclass removing @Inheritance(strategy = InheritanceType.JOINED). In this case, I can see the correct hits in the correct areas.

For reference, my code is similar to that:

@MappedSuperclass
public abstract class ParentObject implements Serializable {
    ....


@Entity
@Cacheable(true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ChildObject extends ParentObject {
    ....

Now in the log I can see:

Cache: com.<...>.ChildObject store hit for com.<...>.ChildObject#52

Instead of:

Cache: com.<...>.ParentObject store hit for com.<...>.ParentObject#52

Of course, changing this annotations has a different behaviour that the one used in the question, and is not the desired one in some cases. But still I think it worth it to let this comment here as future reference. Maybe can help other person.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!