javahibernatenamed-query

java.lang.ClassCastException: [Ljava.lang.Object or LazyInitializationException


I write named query like this

<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>

And when I get result I have a problem. When I write just from TestQuestion I get List<Object[Object[]]> with Question and TestQuestion objects. When I write select tq I get LazyInitializationException. I want to get List but can't.

UPDATED

I decided add other part my 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>

I find that exist 11.4.1.2. Queries that return tuples in documentation of Hibernate documentation

How I should write named query for rignt result?


Solution

  • I don't get your problem.

    Your query (select tq from TestQuestion as tq... and from TestQuestion) should both return List<TestQuestion> instead of List<Object[][]>

    LazyInitializationException normally occur when there is lazy-fetching occurring, but the related session of the entity is closed already (please correct me if I am wrong, just from my bare memory I thought it is LazyInitializationException). It has nothing to do with named query. Please make sure your transaction control etc is setup correctly.