College和Student的one-to-many映射可以参考这篇文章,当访问关联实体的时候,hibernate会查询college下的所有student对象。
@Test
public void testLoad() {
Session session = sessionFactory.openSession();
College college = (College) session.load(College.class, 2);
System.out.println("college name is:" + college.getName());
System.out.println(college.getAllStudents().size());
session.close();
}
使用session.createFilter()能够过滤关联实体的集合。A collection filter is a special type of query that can be applied to a persistent collection or array. The query string can refer to this, meaning the current collection element.
@Test
public void testSessionFilter() {
Session sessio

本文介绍了在Hibernate中如何利用session.createFilter()方法来过滤one-to-many关联关系的集合。通过这种方式,可以对College和Student的关联实体进行定制化的查询,避免加载所有数据,提高效率。注意,传递给createFilter的集合必须是持久态的,并且可以进一步实现如分页查询等功能。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



