我使用带有hibernate的spring数据来实现我的后端,我发现了一个(在我看来)疯狂的事情。
请参阅以下内容:
我的数据模型:
FeedbackAction
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ACTIVITY_TYPE")
@Where(clause = "is_deleted=0")
public abstract class FeedbackAction {
//...
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
private ShoppingItem shoppingItem;
//...
}
ShoppingItem
@Entity
@Where(clause = "is_deleted=0")
public class ShoppingItem {
//...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "shoppingItem")
private List<FeedbackAction> feedbackActions;
//...
}
然后我在我的存储库层中有一个简单的方法。
@Override
public Page<K> findByShoppingItem(ShoppingItem shoppingItem, Pageable pageable);
调用上述方法会导致以下查询(注释是反馈操作的子类):
Hibernate: select count(comment0_.id) as col_0_0_ from feedback_action comment0_ ...
Hibernate: select comment0_.id as id2_1_, ... from feedback_action ...
Hibernate: select user0_.id as id1_5_0_, ... from user user0_ ...
Hibernate: select shoppingit0_.id as id1_3_0_, ... from shopping_item ...
现在疯狂的是,ShoppingItem
(我作为参数发送到存储库方法)再次被查询,尽管我使用
ShoppingItem
实体
ShoppingItem shoppingItem = shoppingItemService.get(shoppingItemID);
在致电
之前repository.findByShoppingItem(shoppingItem, pageable)
为什么会这样?是否有一些注释,或类似的,以避免重复查询?
通过其id检索ShoppingItem实体然后在其上调用.getFeedbackActions()来获取反馈操作,可能有一些可能性来克服这个问题,但我不知道在这种情况下如何使用分页。