延迟加载不起作用 - jpa

发布于 2024-10-15 12:21:34 字数 819 浏览 3 评论 0原文

大家好, 我有三张桌子“儿童”、“宠物”和“玩具”。宠物有一个关于孩子 ID 的参考键,而玩具有一个关于狗 ID 的参考键。 我想加载有关孩子及其宠物的所有数据,但我不想加载

@OneToMany(mappedBy = "petEntity", fetch = FetchType.LAZY)
public Set<PetEntity> getPetEntitySet() {
    return petEntitySet;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ChildId", insertable = false, updatable = false)
public ChildEntity getChildEntity() {
    return childEntity;
}

一组玩具的相同玩具数据。

加载我拥有的数据,

List<ChildEntity> list = entityManager.createQuery(
"SELECT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
 ChildEntity.class).getResultList();
    return list;

但这东西会加载我所有的数据,而不仅仅是有关孩子和他的宠物的信息。

我怎样才能抑制实体管理器只加载表中的数据,而不是在我不想要的时候进行连接

谢谢你的建议,

我忘了提到在这个追逐中它不仅加载所有数据,而且它不仅仅返回子元素的一份副本

Hy all,
I have three tables Child, Pet and Toy. Pet has a reference key the child id, and a toy has a reference key to a dog id.
I want to load all data about a Child and his pets, but i don't want to load the toy data

@OneToMany(mappedBy = "petEntity", fetch = FetchType.LAZY)
public Set<PetEntity> getPetEntitySet() {
    return petEntitySet;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ChildId", insertable = false, updatable = false)
public ChildEntity getChildEntity() {
    return childEntity;
}

same for set of toys.

to load data i have

List<ChildEntity> list = entityManager.createQuery(
"SELECT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
 ChildEntity.class).getResultList();
    return list;

but this thing loads me all data, not just the information about child and his pets.

How can i suppress the entity manager to load only the data in the table, and not to make joins when i don't want that

Thanks for your advices

i forgot to mention that in this chase it doesn't only load all data, but it returns more than just one copy of a child elemnt

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

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

发布评论

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

评论(1

毅然前行 2024-10-22 12:21:34

它加载更多数据的原因是因为您使用了关键字fetch。这将EAGERLY加载孩子的宠物。
只需删除fetch,就会发生延迟加载。

更新

我发现这实际上是您想要的,因此只需确保在所有相关类中正确覆盖equals()hashCode()即可,因为我假设您正在使用Set

通过这种方式,jpa 知道如何查找重复项

第二次更新

是的,您可以在查询中轻松使用DISTINCT

只需在您的选择中添加 DISTINCT

List<ChildEntity> list = entityManager.createQuery(
"SELECT DISTINCT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
 ChildEntity.class).getResultList();
    return list;

The reason why it is loading more data is because you are using the keyword fetch. This will EAGERLY load whatever the child's pets.
Just remove the fetch and lazy loading will occur.

Update

I see that this is actually what you want, so just ensure equals() and hashCode() is correctly overriden in all relevant classes, as I assume you are using Set.

In this way, jpa knows how to look for duplicates

Second update

Yes you can easily use DISTINCT in your queries.

Just add DISTINCT in your select

List<ChildEntity> list = entityManager.createQuery(
"SELECT DISTINCT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
 ChildEntity.class).getResultList();
    return list;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文