有没有一个助手可以知道 Hibernate 是否已加载某个属性?

发布于 2024-08-08 00:31:44 字数 395 浏览 2 评论 0原文

我需要一个帮助程序来了解是否已加载属性,以避免 LazyInitializationException。是否可以?

@Entity
public class Parent {
    @OneToMany
    private List<Child> childList;
}

@Entity
public class Child {

}

"select distinct p from Parent p left join fetch p.childList";

// Answer goes here
// I want to avoid LazyInitializationException
SomeHelper.isLoaded(p.getChildList());

I need a helper to know whether a property has been loaded as a way to avoid LazyInitializationException. Is it possible?

@Entity
public class Parent {
    @OneToMany
    private List<Child> childList;
}

@Entity
public class Child {

}

"select distinct p from Parent p left join fetch p.childList";

// Answer goes here
// I want to avoid LazyInitializationException
SomeHelper.isLoaded(p.getChildList());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扭转时空 2024-08-15 00:31:44

实际上有两种方法。

要查明惰性属性是否已初始化,您可以调用Hibernate.isPropertyInitialized() 方法,以实体实例和属性名称作为参数。

要查明惰性集合(或实体)是否已初始化(如您的示例中所示),您可以调用Hibernate.isInitialized() 以集合(实体)实例作为参数。

There are two methods, actually.

To find out whether a lazy property has been initialized you can invoke Hibernate.isPropertyInitialized() method with your entity instance and property name as parameters.

To find out whether a lazy collection (or entity) has been initialized (like in your example) you can invoke Hibernate.isInitialized() with collection (entity) instance as parameter.

墨落成白 2024-08-15 00:31:44

根据 Hibernate 5.4 的 文档

休眠 API

boolean personInitialized = Hibernate.isInitialized(person);

boolean personBooksInitialized = Hibernate.isInitialized(person.getBooks());

boolean personNameInitialized = Hibernate.isPropertyInitialized(person, "name");

JPA

在 JPA 中,有一种替代方法可以使用以下方法来检查惰性:
遵循 javax.persistence.PersistenceUtil 模式(即
尽可能推荐)。

PersistenceUtil persistenceUnitUtil = Persistence.getPersistenceUtil();

boolean personInitialized = persistenceUnitUtil.isLoaded(person);

boolean personBooksInitialized = persistenceUnitUtil.isLoaded(person.getBooks());

boolean personNameInitialized = persistenceUnitUtil.isLoaded(person, "name");

According to the documentation for Hibernate 5.4

Hibernate API

boolean personInitialized = Hibernate.isInitialized(person);

boolean personBooksInitialized = Hibernate.isInitialized(person.getBooks());

boolean personNameInitialized = Hibernate.isPropertyInitialized(person, "name");

JPA

In JPA there is an alternative means to check laziness using the
following javax.persistence.PersistenceUtil pattern (which is
recommended wherever possible).

PersistenceUtil persistenceUnitUtil = Persistence.getPersistenceUtil();

boolean personInitialized = persistenceUnitUtil.isLoaded(person);

boolean personBooksInitialized = persistenceUnitUtil.isLoaded(person.getBooks());

boolean personNameInitialized = persistenceUnitUtil.isLoaded(person, "name");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文