gpt4 book ai didi

java - 关于 jpa/hibernate subselect 获取的询问

转载 作者:行者123 更新时间:2023-11-30 02:35:52 25 4
gpt4 key购买 nike

我对代码片段 (full source code) 有疑问这是一本有关 Hibernate/JPA 的书籍随附的代码示例。

这是代码 list :

public class Subselect extends JPATest {

@Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("FetchingSubselectPU");
}

public FetchTestData storeTestData() throws Exception {
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = JPA.createEntityManager();

Long[] itemIds = new Long[3];
Long[] userIds = new Long[3];

User johndoe = new User("johndoe");
em.persist(johndoe);
userIds[0] = johndoe.getId();

User janeroe = new User("janeroe");
em.persist(janeroe);
userIds[1] = janeroe.getId();

User robertdoe = new User("robertdoe");
em.persist(robertdoe);
userIds[2] = robertdoe.getId();

Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
em.persist(item);
itemIds[0] = item.getId();
for (int i = 1; i <= 3; i++) {
Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
item.getBids().add(bid);
em.persist(bid);
}

item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
em.persist(item);
itemIds[1] = item.getId();
for (int i = 1; i <= 1; i++) {
Bid bid = new Bid(item, janeroe, new BigDecimal(2 + i));
item.getBids().add(bid);
em.persist(bid);
}

item = new Item("Item Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
em.persist(item);
itemIds[2] = item.getId();
for (int i = 1; i <= 1; i++) {
Bid bid = new Bid(item, johndoe, new BigDecimal(3 + i));
item.getBids().add(bid);
em.persist(bid);
}

tx.commit();
em.close();

FetchTestData testData = new FetchTestData();
testData.items = new TestData(itemIds);
testData.users = new TestData(userIds);
return testData;
}

@Test
public void fetchCollectionSubselect() throws Exception {
storeTestData();

UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();

List<Item> items = em.createQuery("select i from Item i").getResultList();
// select * from ITEM

for (Item item : items) {
assertTrue(item.getBids().size() > 0);
// select * from BID where ITEM_ID in (
// select ID from ITEM
// )
}

// The actual test
em.clear();
items = em.createQuery("select i from Item i").getResultList();
// Access should load all collections
assertTrue(items.iterator().next().getBids().size() > 0);
em.clear(); // Detach all
for (Item item : items) {
assertTrue(item.getBids().size() > 0);
}

tx.commit();
em.close();
} finally {
TM.rollback();
}
}

}

我不明白的具体部分是这个:

for (Item item : items) {
assertTrue(item.getBids().size() > 0);
// select * from BID where ITEM_ID in (
// select ID from ITEM
// )
}

它旨在演示 hibernate 的预取与子选择功能的使用以及将要执行的 SQL 查询。

以下注释://select ID from ITEM 表示所有项目 ID 均从数据库中检索。这就是子选择预取的作用吗?为什么要从 ITEM 表中检索所有 ID

这是Item实体:

@Entity
public class Item {
@OneToMany(mappedBy = "item")
@org.hibernate.annotations.Fetch(
org.hibernate.annotations.FetchMode.SUBSELECT
)
protected Set<Bid> bids = new HashSet<>();
// ...
}

最佳答案

子选择预取是指在加载关联的子项时,将用于加载父项的原始查询用作子查询,以便一次性初始化所有加载的父项中的子项。您加载父级的查询是select i from Item i(您加载所有项目),因此它用作出价的子查询(转换为仅投影项目的ID)。

如果您的原始查询是select i from Item i where i.someProperty = :something,则加载出价时的子查询将为select ID from ITEM where SOME_PROPERTY_COLUMN = :something.

关于java - 关于 jpa/hibernate subselect 获取的询问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43093165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com