hql按几个属性选择组的最大值(mysql)

时间:2012-12-14 22:13:30

标签: java mysql hibernate nhibernate hql

我尝试编写查询以获取主题ID的最新版本测试的TestQuestion对。

from TestQuestion as tq inner join fetch tq.question
where tq.testQuestionIdentifer = (
    select new TestQuestionIdentifer(new_versId, new_testId) 
    from (
        select 
            max(test_inner.testIdentifer.versionId) as new_versId,
            test_inner.testIdentifer.testId as new_testId 
        from Test test_inner
        where test_inner.subject.subjectId=:subjectId
    )
)

映射

<hibernate-mapping>
    <class name="by.bsuir.testapp.model.TestQuestion" table="TEST_QUESTION">
        <composite-id name="testQuestionIdentifer"
            class="by.bsuir.testapp.model.TestQuestionIdentifer">
            <key-property name="testId" column="TEST_ID" />
            <key-property name="versionId" column="VERSION_ID" />
            <key-property name="questionId" column="QUESTION_ID" />
        </composite-id>
        <many-to-one name="question" class="by.bsuir.testapp.model.Question"
            insert="false" update="false" lazy="false">
            <column name="QUESTION_ID" />
        </many-to-one>
    </class>

我尝试了不同的变种,但最后。是否可以在HQL或SQL中编写此查询?

0 个答案:

没有答案