2012-09-17 7 views
38

mam wyboru dwóch kolumn id jednak uzyskać określony błąd:zapytań określony dołączyć sprowadzanie, ale właściciel pobranego stowarzyszenia nie był obecny na liście select

org.hibernate.QueryException: **query specified join fetching, but the owner of the fetched association was not present in the select list** 

[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=r,role=null,tableName=REVISIONS,tableAlias=revision1_,origin=ENTITY_CHANGED_IN_REVISION entitychan0_,columns={entitychan0_.REV_ID ,className=ru.csbi.registry.domain.envers.Revision}}] [ select ec.id as entityChangeId, r.id as revisionId from ru.csbi.registry.domain.envers.EntityChange as ec inner join fetch ec.revision as r where ec.groupEntityId = :groupEntityId and ec.groupName = :groupName and r.timestamp < :entityDateFrom and r.timestamp > :entityDateTo and (  ec.revisionType in (0, 5, 1, 4, 2)  and not (ec.otherGroupEntityModified = false and ec.thisGroupEntityModified = true and ec.rowDataModified = false and ec.collectionOfNotGroupEntityModified = false )  ) group by ec.id, r.id having count(*) > :start order by r.id desc] 

Niektóre kodu:

String hql = " select ec.id as entityChangeId, r.id as revisionId from EntityChange as ec " + 
      " inner join fetch ec.revision as r " + 
      " where ec.groupEntityId = :groupEntityId" + 
      " and ec.groupName = :groupName " + 
      " and r.timestamp < :entityDateFrom " + 
      " and r.timestamp > :entityDateTo " + 
      " and (" + 
      "  ec.revisionType in (" + 
         RevisionType.ADD.getRepresentation() + ", " + 
         RevisionType.ONLY_DATA_PROPERTY_MOD.getRepresentation() + ", " + 
         RevisionType.BOTH_COLLECTION_AND_PROPERTY_MOD.getRepresentation() + ", " + 
         RevisionType.ONLY_COLLECTION_PROPERTY_MOD.getRepresentation() + ", " + 
         RevisionType.DEL.getRepresentation() + 
        ") " + 
      "  and not ("+ 
        "ec.otherGroupEntityModified = false and " + 
        "ec.thisGroupEntityModified = true and " + 
        "ec.rowDataModified = false and " + 
        "ec.collectionOfNotGroupEntityModified = false " + 
       " ) " + 
      " ) " + 
      " group by ec.id, r.id " + 
      " having count(*) > :start" + 
      " order by r.id desc"; 

Jak naprawić błąd i co robię źle?

+2

dla przyszłych poszukiwaczy tego pytania, w mojej sytuacji dołączyłem do atrybutu "nie leniwy". Po usunięciu klauzuli dołączania został rozwiązany. – merveotesi

Odpowiedz

71

regularne join zamiast join fetch (nawiasem mówiąc, to inner domyślnie):

String hql = " select ec.id as entityChangeId, r.id as revisionId from EntityChange as ec " + 
     " join ec.revision as r " + ... 

jako komunikat o błędzie informuje, join fetch nie ma sensu tutaj, ponieważ jest to wskazówka wydajność, która zmusza chętny ładowanie kolekcji.

+4

Aby wyjaśnić, 'join fetch' może być również użyty do wymuszenia szybkiego ładowania powiązania, takiego jak pole z adnotacją @ManyToOne, a nie tylko kolekcje. –

+14

Hm .. Mam do czynienia z podobnym problemem, ale muszę wykonać sprzężenie, aby uniknąć problemu z n + 1. – Ievgen

+0

@levgen, Nie znam szczegółów Twojego zapytania, ale pamiętaj, że nie powinno się podawać kwerendy "przynieś" w ogóle. https://codingexplained.com/coding/java/spring-framework/fetch-query-not-working-spring-data-jpa-pageable#comment-293535 –

Powiązane problemy