【问题标题】:java.lang.ClassCastException: [Ljava.lang.Object or LazyInitializationExceptionjava.lang.ClassCastException:[Ljava.lang.Object 或 LazyInitializationException
【发布时间】:2012-11-13 21:15:08
【问题描述】:

我这样写命名查询

<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&lt;Object[Object[]]&gt; 和 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>

我发现在 Hibernate documentation 的文档中存在 11.4.1.2. Queries that return tuples

我应该如何为 rignt 结果编写命名查询?

【问题讨论】:

    标签: java hibernate named-query


    【解决方案1】:

    我不明白你的问题。

    您的查询(select tq from TestQuestion as tq...from TestQuestion)都应该返回 List&lt;TestQuestion&gt; 而不是 List&lt;Object[][]&gt;

    LazyInitializationException 通常在发生延迟获取时发生,但实体的相关会话已经关闭(如果我错了,请纠正我,仅凭我的记忆,我认为它是LazyInitializationException)。它与命名查询无关。请确保您的交易控制等设置正确。

    【讨论】:

    • 我没有得到 List 的原因是什么?
    • 根据文档,这个结果意味着查询返回元组而不是 List
    • 你已经映射了 TestQuestion,你正在选择实体,select 子句只包含一个实体,你正在编写 HQL.... 然后你应该得到 List 而不是元组。你认为你会通过什么方式得到 List 而不是 List
    • 也许我误解了你的意思:你的意思是你实际上得到了元组而不是 List 对吧? (我认为你认为你应该得到元组,但你没有)你的查询似乎没有导致问题,你是否尝试过直接通过会话查询而不是使用命名查询?引用的命名查询是您的实际查询吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2014-05-09
    • 2015-02-01
    • 2014-07-08
    • 2012-08-07
    相关资源
    最近更新 更多