2017-08-03 223 views
0

基於服務(收藏單位)條件services.get爲什麼條件查詢不過濾的記錄(「狀態」),「待定」)如下?JPA標準篩選集合

CriteriaQuery<Customer> query = cb.createQuery(Customer.class); 
    Root<Customer> customer = query.from(Customer.class); 
Join<Customer, Service> services = customer.join("services", JoinType.INNER); 

List<Predicate> predicates = new ArrayList<Predicate>(); 

predicates.add(cb.equal(customer.get("customerId"), 1)); 
predicates.add(cb.equal(services.get("status"), "pending")); 

query.select(customer).distinct(true) 
     .where(predicates.toArray(new Predicate[]{})); 

List<Customer> customers = em.createQuery(query).getResultList(); 

,其中作爲SQL的功能篩選記錄正確

select * from customers c 
    INNER JOIN SERVICES s on s.COID = c.COID 
    where c.ID=1 and 
    s.status='pending'; 

記錄是在結果集基於狀態條件(收集)沒有資格,其實,返回了客戶的所有服務。

我試圖用fetch連接(因爲有客戶和2該客戶的服務執行的第一個2個查詢,認爲狀態可能會在第二次查詢中未評估)使用

customer.fetch("services", JoinType.INNER); 

,但沒有運氣。

我很驚訝這種行爲。我正在使用OpenJPA JPA供應商

實體爲客戶服務

public class Customer{ 
    @Id 
    @Column(name = "ID") 
    private Integer customerId; 
    @OneToMany 
    @MappedBy(name = "customer") 
    private List<Service> services; 
    } 

public class Service { 
    @EmbeddedId 
    private ServicesPK servicePK; 
    @ManyToOne 
    @JoinColumn(name = "COID") 
    private Customer customer; 
    } 

@Embeddable 
@EqualsAndHashCode 
public class ServicesPK implements Serializable { 
    @Column(name = "COID") 
    private Integer coId; 
    @Column(name = "VERSION") 
    private Integer version; 
} 
+0

@wypieprz能否請你看看這個問題呢?謝謝 –

回答

0

試試這個代碼,改變我所做的是我添加類型安全有關的類型安全的query.Know typesafe

CriteriaQuery<Customer> query = cb.createQuery(Customer.class); 
Root<Customer> customer = query.from(Customer.class); 
Join<Customer, Service> services = customer.join(Customer_.services); 
List<Predicate> predicates = new ArrayList<Predicate>(); 

predicates.add(cb.equal(customer.get("customerId"), 1)); 
predicates.add(cb.equal(services.get(Service_.status), "pending")); 

query.select(customer).distinct(true) 
    .where(predicates.toArray(new Predicate[]{})); 

List<Customer> customers = em.createQuery(query).getResultList(); 
+0

我已經試過這種方式,但沒有運氣。 :( –