Hibernate Native SQL Query retrieving entities and collections

前端 未结 5 1641
谎友^
谎友^ 2020-12-13 06:54

This is my situation, I have two basic POJO\'s which I\'ve given a simple hibernate mapping :

Person
  - PersonId
  - Name
  - Books

Book
  - Code
  - Descr         


        
5条回答
  •  心在旅途
    2020-12-13 07:49

    The following works for me:

    session.createSQLQuery("select p.*, b.* from person p, book b where ").
    .addEntity("person", Person.class).addJoin("book", "person.books").list();
    

    This returns an Object[] containing a list of Person, each of which contains a list of Books. It does this in a single SQL select. I think your problem is that you don't specifically alias person to anything.

    EDIT: The method returns an Object[], but the array is populated with Person instances, and only Person instances.

    If Hibernate doesn't understand how to map to your classes or if it can't understand how to map the join, it will return a list of objects. Make sure you only have one Person/Book combination on each line.

提交回复
热议问题