2012-09-17 5 views
38

sto selezionando due colonne id ma ottengo errore specificato:query specificata unirsi recupero, ma il proprietario dell'associazione scaricata non era presente nella lista di selezione

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] 

Alcuni codice:

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

Come correggere l'errore e cosa sto facendo male?

+2

per i futuri utenti di questa domanda, nella mia situazione, mi stavo unendo a un attributo non pigro. Quando ho rimosso la clausola di adesione è stato risolto. – merveotesi

risposta

71

uso regolare join invece di join fetch (a proposito, è inner per impostazione predefinita):

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

Come messaggio di errore che dice, join fetch non ha senso qui, perché è un indizio delle prestazioni che costringe ansiosi caricamento della raccolta.

+4

Giusto per chiarire, 'join fetch' può anche essere usato per forzare il caricamento impaziente di un'associazione come un campo annotato con @ManyToOne e non solo collezioni. –

+14

Hm .. Sto affrontando un problema simile, ma ho bisogno di fare il recupero di join per evitare n + 1 problema – Ievgen

+0

@levgen, non conosco i dettagli della tua query ma, tieni presente che la query di conteggio non dovrebbe avere "prendi" del tutto. https://codingexplained.com/coding/java/spring-framework/fetch-query-not-working-spring-data-jpa-pageable#comment-293535 –