【问题标题】:How can I tell if a Hibernate entity is read-only or not如何判断 Hibernate 实体是否为只读
【发布时间】:2011-12-07 17:50:50
【问题描述】:

我在检索实体后将其设为只读:

Session session = (Session)entityManager.getDelegate();
session.setReadOnly( entity, makeReadOnly );

我希望能够以编程方式测试该对象及其延迟加载的子对象是否实际上是只读的,但我没有看到任何 API 可以做到这一点。有这样的API吗?

更新:我使用的是 Hibernate 3.3.1

【问题讨论】:

  • 只是好奇,这是用于单元测试还是您希望在实时代码中使用它?为什么需要它?
  • @aishwarya,我想验证当父级设置为只读时延迟加载的实体是如何工作的。我还想看看子实体在将父实体设置为只读之前是否已经加载(无论是否延迟加载)的行为。我基本上只在调试一些代码时才需要它。

标签: java hibernate persistence


【解决方案1】:

Session.isReadOnly(Object entityOrProxy)

编辑: 3.3.1 的丑陋破解:

//usual imports
//....
import org.hibernate.impl.SessionImpl;
import org.hibernate.engine.EntityEntry;
import org.hibernate.engine.Status;
import org.hibernate.engine.StatefulPersistenceContext;


Session session = (Session)entityManager.getDelegate();

SessionImpl sessImpl = (SessionImpl) session;
if (((StatefulPersistenceContext) sessImpl.getPersistenceContext()).getEntry(entity).getStatus() == Status.READ_ONLY) {
    dosomething
};

【讨论】:

  • 啊.. 3.5 中就有,但我们正在使用的 3.3.1 中没有,但我可以查看 3.5 实现中的代码并使用它。谢谢..
  • @ChrisWilliams 啊,好吧不知道它是 3.3.1。编辑了我的答案,但它包含很多丑陋的演员表。
  • 我应该在接受后回来查看!我刚回来发布我的 3.3.1 解决方案,你打败了我。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多