如何创建一个每次请求时都会重新读取的hibernate集合?

时间:2021-05-19 03:49:39

I have an entity that has a state table associated with it. The state table is managed by another process, and contains a list of objects that my business logic must process. I would like to get a new snapshot of the state table each time I reload the entity. How can I ensure that no part of Hibernate or its support libraries ever caches any of the values of this table? Basically, I want to get a new view of the collection every time I call getMyStateValues ().

我有一个实体,它有一个与之关联的状态表。状态表由另一个进程管理,并包含我的业务逻辑必须处理的对象列表。每次重新加载实体时,我想获得状态表的新快照。如何确保Hibernate或其支持库的任何部分都不会缓存此表的任何值?基本上,我希望每次调用getMyStateValues()时都能获得集合的新视图。

3 个解决方案

#1


Most of the point of Hibernate is to prevent that from happening and return a consistent view of an entity's state in the scope of a given transaction. So either reload the whole entity, in different transactions every time. Or, if you need to reload the state table during a business transaction, load only the state table by the parent entity's id in a separate Hibernate session.

Hibernate的大部分要点是防止这种情况发生,并在给定事务的范围内返回实体状态的一致视图。因此要么每次都在不同的事务中重新加载整个实体。或者,如果需要在业务事务期间重新加载状态表,请在单独的Hibernate会话中仅通过父实体的id加载状态表。

#2


You can create a method in your entity that queries the database and return the collection. getXYXReload(). It´s not a very nice design decision, thought.

您可以在实体中创建查询数据库并返回集合的方法。 getXYXReload()。想到这不是一个非常好的设计决定。

#3


You can use Hibernate's CacheMode. It allows you to instruct a hibernate session on how to interact with the cache. You can get access to the underlying session with:

您可以使用Hibernate的CacheMode。它允许您指示如何与缓存交互的休眠会话。您可以通过以下方式访问基础会话:

@PersistenceContext EntityManager manager;
...
org.hibernate.Session session = (Session)manager.getDelegate();

Unfortunately, this technique applies to the whole session, and not specifically to an entity.

不幸的是,这种技术适用于整个会话,而不是特定于实体。

#1


Most of the point of Hibernate is to prevent that from happening and return a consistent view of an entity's state in the scope of a given transaction. So either reload the whole entity, in different transactions every time. Or, if you need to reload the state table during a business transaction, load only the state table by the parent entity's id in a separate Hibernate session.

Hibernate的大部分要点是防止这种情况发生,并在给定事务的范围内返回实体状态的一致视图。因此要么每次都在不同的事务中重新加载整个实体。或者,如果需要在业务事务期间重新加载状态表,请在单独的Hibernate会话中仅通过父实体的id加载状态表。

#2


You can create a method in your entity that queries the database and return the collection. getXYXReload(). It´s not a very nice design decision, thought.

您可以在实体中创建查询数据库并返回集合的方法。 getXYXReload()。想到这不是一个非常好的设计决定。

#3


You can use Hibernate's CacheMode. It allows you to instruct a hibernate session on how to interact with the cache. You can get access to the underlying session with:

您可以使用Hibernate的CacheMode。它允许您指示如何与缓存交互的休眠会话。您可以通过以下方式访问基础会话:

@PersistenceContext EntityManager manager;
...
org.hibernate.Session session = (Session)manager.getDelegate();

Unfortunately, this technique applies to the whole session, and not specifically to an entity.

不幸的是,这种技术适用于整个会话,而不是特定于实体。