2

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?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Ray
  • 1,788
  • 7
  • 55
  • 92

1 Answers1

0

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.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • What can be the reason that I get not List ? – Ray Nov 14 '12 at 12:48
  • In accoring with documentation this result means that query return tuples instead of List – Ray Nov 14 '12 at 13:05
  • You have TestQuestion mapped, you are selecting the entity, the select clause contains only one entity, you are writing HQL.... Then you should get List instead of tuples. By what means you think you will get List instead of List ? – Adrian Shum Nov 15 '12 at 01:39
  • Maybe I have misunderstood what you mean: You mean you actually get the tuples instead of List right? (I thought you think you should get the tuples but you are not) Your query doesn't seems causing the problem, have you tried to query directly through session instead of using Named query? Is the quoted named query being your actual query? – Adrian Shum Nov 15 '12 at 01:46