使用 JPA2 在 Hibernate 中急切加载 OneToMany

发布于 2024-09-02 00:36:47 字数 1712 浏览 10 评论 0原文

我在 PersonPet 实体之间有一个简单的 @OneToMany

@OneToMany(mappedBy="owner", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Pet> getPets() { return pets; }

我想加载所有具有关联的 Person 宠物。所以我想出了这个(在测试类中):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AppTest {

    @Test
    @Rollback(false)
    @Transactional(readOnly = false)
    public void testApp() {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Person> c = qb.createQuery(Person.class);
        Root<Person> p1 = c.from(Person.class);
        SetJoin<Person, Pet> join = p1.join(Person_.pets);
        TypedQuery<Person> q = em.createQuery(c);
        List<Person> persons = q.getResultList();
        for (Person p : persons) {
            System.out.println(p.getName());
            for (Pet pet : p.getPets()) {
                System.out.println("\t" + pet.getNick());
            }
        }

但是,打开 SQL 日志记录显示它执行 3 个查询(数据库中有 2 个人)。

Hibernate: select person0_.id as id0_, person0_.name as name0_, person0_.sex as sex0_ from Person person0_ inner join Pet pets1_ on person0_.id=pets1_.owner_id
Hibernate: select pets0_.owner_id as owner3_0_1_, pets0_.id as id1_, pets0_.id as id1_0_, pets0_.nick as nick1_0_, pets0_.owner_id as owner3_1_0_ from Pet pets0_ where pets0_.owner_id=?
Hibernate: select pets0_.owner_id as owner3_0_1_, pets0_.id as id1_, pets0_.id as id1_0_, pets0_.nick as nick1_0_, pets0_.owner_id as owner3_1_0_ from Pet pets0_ where pets0_.owner_id=?

有什么建议吗?

谢谢 格尔戈

I have a simple @OneToMany between Person and Pet entities:

@OneToMany(mappedBy="owner", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Pet> getPets() { return pets; }

I would like to load all Persons with associated Pets. So I came up with this (inside a test class):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AppTest {

    @Test
    @Rollback(false)
    @Transactional(readOnly = false)
    public void testApp() {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Person> c = qb.createQuery(Person.class);
        Root<Person> p1 = c.from(Person.class);
        SetJoin<Person, Pet> join = p1.join(Person_.pets);
        TypedQuery<Person> q = em.createQuery(c);
        List<Person> persons = q.getResultList();
        for (Person p : persons) {
            System.out.println(p.getName());
            for (Pet pet : p.getPets()) {
                System.out.println("\t" + pet.getNick());
            }
        }

However, turning the SQL logging on shows, that it executes 3 queries (having 2 Persons in the DB).

Hibernate: select person0_.id as id0_, person0_.name as name0_, person0_.sex as sex0_ from Person person0_ inner join Pet pets1_ on person0_.id=pets1_.owner_id
Hibernate: select pets0_.owner_id as owner3_0_1_, pets0_.id as id1_, pets0_.id as id1_0_, pets0_.nick as nick1_0_, pets0_.owner_id as owner3_1_0_ from Pet pets0_ where pets0_.owner_id=?
Hibernate: select pets0_.owner_id as owner3_0_1_, pets0_.id as id1_, pets0_.id as id1_0_, pets0_.nick as nick1_0_, pets0_.owner_id as owner3_1_0_ from Pet pets0_ where pets0_.owner_id=?

Any tips?

Thanks
Gergo

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

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

发布评论

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

评论(1

彼岸花ソ最美的依靠 2024-09-09 00:36:48

而不是

SetJoin<Person, Pet> join = p1.join(Person_.pets);

应该写

p1.fetch(Person_.pets);

Instead of

SetJoin<Person, Pet> join = p1.join(Person_.pets);

one should write

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