2015-10-03 72 views
1

我正在使用Query DSL與版本4.0.4這裏是我的依賴關係;如何從QueryDSL檢索列表/結果

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-apt</artifactId> 
    <version>4.0.4</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-jpa</artifactId> 
    <version>4.0.4</version> 
</dependency> 

<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-log4j12</artifactId> 
    <version>1.6.1</version> 
</dependency> 

這是我的QueryDSL查詢的一個片段。

 JPQLQuery query = new JPAQuery(em); 
     QQuotation quotation = QQuotation.quotation; 
     query.from(quotation).where(quotation.ticketNumber.like("this")) 

然而,查詢方法沒有一個list()方法 當我行.where(quotation.ticketNumber.like("this"))

下面是完整的代碼。

import com.querydsl.jpa.JPQLQuery; 
import com.querydsl.jpa.impl.JPAQuery; 
import org.app.now.domain.process.QQuotation; 
import org.app.now.domain.process.Quotation; 
import org.app.now.repo.QuotationRepoCustom; 
import org.joda.time.LocalDateTime; 
import org.springframework.beans.factory.annotation.Autowired; 

import javax.persistence.EntityManager; 
import java.util.List; 

public class QuotationRepoImpl implements QuotationRepoCustom { 

    @Autowired 
    private EntityManager em; 

    @Override 
    public List<Quotation> search(String ticketNumber, String description, LocalDateTime startDate, LocalDateTime endDate) { 
     System.out.println("Searching"); 
     JPQLQuery query = new JPAQuery(em); 
     QQuotation quotation = QQuotation.quotation; 
     query.from(quotation).where(quotation.ticketNumber.like("this")). 
     return null; 
    } 
} 

回答

1

看看Fechable接口。

@Override 
public List<Quotation> search(String ticketNumber, String description, LocalDateTime startDate, LocalDateTime endDate) { 
    System.out.println("Searching"); 
    JPQLQuery query = new JPAQuery(em); 
    QQuotation quotation = QQuotation.quotation; 
    return query.from(quotation).where(quotation.ticketNumber.like("this")).fetch(); 
} 

祝你好運!

0

像這樣

@Override 
public List<Quotation> search(String ticketNumber, String description, LocalDateTime startDate, LocalDateTime endDate) { 
    JPQLQuery<Void> query = new JPAQuery<Void>(em); 
    QQuotation quotation = QQuotation.quotation; 
    return query.select(quotation) 
       .from(quotation) 
       .where(quotation.ticketNumber.like("this")) 
       .fetch(); 
}