我这样写命名查询
<query name="GET_QUESTIONS_BY_TEST_ID">select tq from TestQuestion as tq inner join tq.question as
q
where
tq.testQuestionIdentifer.versionId=(select
max(tq.testQuestionIdentifer.versionId) from TestQuestion tq_inner
where
tq_inner.testQuestionIdentifer.testId=:testId) and
tq.testQuestionIdentifer.testId=:testId
</query>
当我得到结果时,我遇到了问题。当我只写 from TestQuestion
我得到 List<Object[Object[]]>
与 Question 和 TestQuestion 对象。当我写 select tq
我得到 LazyInitializationException
.我想获取 List 但获取不到。
已更新
我决定添加其他部分我的 hbm
<class name="by.bsuir.testapp.model.TestQuestion" table="TEST_QUESTION"
lazy="true">
<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"
fetch="join" insert="false" update="false">
<column name="QUESTION_ID" />
</many-to-one>
</class>
我发现存在 11.4.1.2. Queries that return tuples
在 Hibernate 文档中 documentation
如何为正确的结果编写命名查询?
最佳答案
我不明白你的问题。
您的查询(select tq from TestQuestion as tq...
和 from TestQuestion
)应该都返回 List<TestQuestion>
而不是 List<Object[][]>
LazyInitializationException
通常发生在延迟获取发生时,但是实体的相关 session 已经关闭(如果我错了请纠正我,仅凭我的内存我认为它是 LazyInitializationException
)。它与命名查询无关。请确保您的交易控制等设置正确。
关于java.lang.ClassCastException : [Ljava. lang.Object 或 LazyInitializationException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13368922/