首页 > 解决方案 > 如何限制我的域对象中的集合

问题描述

该代码使用(旧式)休眠检索对象:

Session session = super.getSession();
Student  results = (Student) session.get(Student.class, id);

Student 对象在其中包含一组孩子的成就。

<hibernate-mapping>
<class name="edu.texas.Student" table="STUDENT" >
    <property name="createUser" type="string">
        <column name="CREATE_USER" length="25" />
    </property>

    <set name="achievements" inverse="true" lazy="true" table="ACHIEVEMENTS" fetch="select">
        <key>
            <column name="ACHIEVEMENT_ID" length="25" not-null="true" />
        </key>
        <one-to-many class="edu.texas.Achievements" />
    </set>

我希望成就集受到日期范围的限制(例如,仅限于今年的成就,而不是孩子的整个职业生涯)。

我开始对 Set 的对象使用标准,但标准想要返回一个列表。

在这种情况下,该列表位于我的 Student 对象内。

Criteria criteria = session.createCriteria(Student.class)
                     .createAlias("achievements", "a")
                     .add( Restrictions.between("a.createTs", startDate, endDate ) );
results = (Student) crit.uniqueResult();

标签: javahibernate

解决方案


推荐阅读