2013-02-19 60 views
7

मैं क्रम में वसंत डाटा में JPQL का उपयोग कर डेटाबेस के लिए कई प्रश्नों से बचने और में शामिल होने में लाएं का उपयोग एक क्वेरी में जानकारी के सभी पुनः प्राप्त करने के एक मूल क्वेरी अनुकूलन करने के लिए कोशिश कर रहा हूँ और मैं लायेंJPQL शामिल हों वसंत में ManyToOne सहयोग से काम नहीं कर रहा डाटा

org.springframework.dao.InvalidDataAccessApiUsageException: 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=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id ,className=com.medifast.entity.core.Country}}] [select count(ca) from com.medifast.entity.core.CoApplicant ca LEFT JOIN FETCH ca.country LEFT JOIN FETCH ca.state where ca.client.id = :clientId]; nested exception is java.lang.IllegalArgumentException: 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=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id ,className=com.medifast.entity.core.Country}}] [select count(ca) from com.medifast.entity.core.CoApplicant ca LEFT JOIN FETCH ca.country LEFT JOIN FETCH ca.state where ca.client.id = :clientId] 
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301) 

यह मेरा दाव है:

/** 
* Interface to handle persistence operations for CoApplicants. 
* 
*/ 
@Repository 
public interface ICoApplicantDao extends JpaRepository<CoApplicant, Long> 
{ 
    @Query("select ca from CoApplicant ca JOIN FETCH ca.country c where ca.client.id = :clientId") 
    Page<CoApplicant> findCoApplicantsByClient(@Param("clientId") Long clientId, Pageable pageable); 
} 

और ये मेरी संस्थाओं हैं:

@Entity 
@Table(name = "BUSINESS.COAPPLICANTS") 
@SQLDelete(sql = "UPDATE BUSINESS.COAPPLICANTS SET DELETED = 1 WHERE id = ?") 
@Where(clause = "DELETED <> 1") 
@Data 
@EqualsAndHashCode(callSuper = true) 
public class CoApplicant extends AbstractAuditableEntity 
{ 

    /** 
    * Serial. 
    */ 
    private static final long serialVersionUID = -297231024073091062L; 


    /** 
    * First name. 
    */ 
    @Column(name = "FirstName") 
    private String firstName; 

    /** 
    * Last name. 
    */ 
    @Column(name = "LastName") 
    private String lastName; 


    /** 
    * Recognition Name. 
    */ 
    private String recognitionName; 

    /** 
    * For the address line 1 field. 
    */ 
    private String addressLine1; 

    /** 
    * For the address line 2 field. 
    */ 
    private String addressLine2; 


    /** 
    * City. 
    */ 
    private String city; 

    /** 
    * State. 
    */ 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "State_Id") 
    private State state; 

    /** 
    * Zip Code. 
    */ 
    private String zipCode; 

    /** 
    * Country. 
    */ 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "Country_Id") 
    private Country country; 

    /** 
    * Email address. 
    */ 
    @Column(unique = true) 
    private String email; 

    /** 
    * Main Phone number. 
    */ 
    private String mainPhone; 

    /** 
    * Constructor. 
    */ 
    public CoApplicant() 
    { 
     super(); 
    } 
} 


/** 
* Country entity. 
*/ 
@Entity 
@Table(name = "Business.Countries") 
@SQLDelete(sql = "Update Business.Countries set deleted = 1 where id=?") 
@Where(clause = "deleted <> 1") 
@Data 
@EqualsAndHashCode(callSuper = true) 
public class Country extends AbstractAuditableEntity 
{ 

    /** 
    * Serial. 
    */ 
    private static final long serialVersionUID = -267110442898674427L; 

    /** 
    * Name of the country. 
    */ 
    private String name; 

    /** 
    * The orders for the client. 
    */ 
    @OneToMany(mappedBy = "country", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
    private Collection<State> states; 

    /** 
    * Const. 
    */ 
    public Country() 
    { 
     super(); 
    }  

} 


    /** 
* State entity. 
*/ 
@Entity 
@Table(name = "Business.States") 
@SQLDelete(sql = "Update Business.States set deleted = 1 where id=?") 
@Where(clause = "deleted <> 1") 
@Data 
@EqualsAndHashCode(callSuper = true) 
public class State extends AbstractAuditableEntity 
{ 

    /** 
    * Serial. 
    */ 
    private static final long serialVersionUID = 8643487990581006632L; 

    /** 
    * Name of the state. 
    */ 
    private String name; 

    /** 
    * Code of the state. 
    */ 
    private String code; 

    /** 
    * Country to which this State belongs. 
    */ 
    @ManyToOne 
    @JoinColumn(name = "Country_Id") 
    private Country country; 


    /** 
    * Constr. 
    */ 
    public State() 
    { 
     super(); 
    } 
} 
इस अपवाद मिलती रहती है

किसी भी अंतर्दृष्टि, मदद अत्यधिक सराहना की जाएगी।

मैं क्या हासिल करना कुछ इस तरह दिखेगा चाहते हैं के लिए शुद्ध एसक्यूएल:

SELECT ca.Id 
     ,firstName 
     ,lastName 
     ,recognitionName 
     ,addressLine1 
     ,city 
     ,email 
     ,mainPhone 
     ,coun.name 
     ,sta.name 
    FROM coApplicants AS ca 
    LEFT JOIN countries AS coun 
    on ca.country_Id=coun.id 
    LEFT JOIN states AS sta 
    on ca.state_Id=sta.id 
+0

कैसे (और क्यों) आप एक बैग लाने, जबकि सिर्फ गिनती लौटने (शामिल हो सकते हैं)? –

+0

अरे गुइडो, बस सोच क्यों एक बैग यहाँ, हर CoApplicant केवल एक राज्य और केवल एक ही इसे करने के लिए जुड़े देश होना चाहिए। मैं क्या बिल्कुल ठीक काम करता है पूरा करने के लिए चाहते हैं के लिए निम्नलिखित शुद्ध SQL क्वेरी: चयन ca.Id , firstName , lastName , recognitionName , addressLine1 , शहर , ईमेल , mainPhone , coun.name , sta.name coApplicants के रूप में सीए बाएं से देश के रूप में शामिल देशों ca.country_Id पर \t = coun.id वाम राज्यों स्टेशन के रूप में शामिल \t ca.state_Id = sta.id – vanvasquez

+2

मैं एक ही मुद्दा है पर, समस्या के साथ है " पेज "आईएनजी और" फ़ेच "यदि आप इसे सूची के साथ बदलते हैं तो यह पूरी तरह से काम करेगा। आप, समाधान खोजने की थी यदि हाँ तो कृपया हमें :) के साथ साझा –

उत्तर

10

एक pagable क्वेरी का उपयोग के साथ लाने में शामिल होने के हाइबरनेट के लिए एक मुद्दा है।

स्थिति का समाधान करने के लिए आप countQuery जगह है - मूल रूप में लगभग एक ही है, लेकिन असंबंधित अंदरूनी मिलती है (अंततः को हासिल करेगा) निकाल दिए जाते हैं।

समाधान के स्रोत यहां पाया जा सकता है: Spring-Data FETCH JOIN with Paging is not working

तो आप के लिए समाधान हो जाएगा:

@Query(
    value="select ca from CoApplicant ca JOIN FETCH ca.country c where ca.client.id = :clientId", 
    countQuery="select count(ca) from CoApplicant ca where ca.client.id = :clientId") 
Page<CoApplicant> findCoApplicantsByClient(@Param("clientId") Long clientId, Pageable pageable); 
+0

धन्यवाद एक बहुत joro, आप क्या काम करता है पूरी तरह से सुझाव दिया। – vanvasquez

+0

अंत में, मुझे यह मिला। आपका बहुत बहुत धन्यवाद! –

+0

लेकिन हम @query –