2012-03-16 14 views
5

Vorrei sapere se è possibile avere subquery in un'annotazione @query (org.springframework.data.jpa.repository.Query;)SpringData: è possibile avere subquery nell'annotazione Query?

sto ottenendo un QuerySyntaxException al primo Parentesis sottoquery.

Ecco la mia domanda

@Query(value="select c1 from ComplaintModel c1, " 
+ "(select c2.id, min(cb.termDate) minDate from ComplaintModel c2 " 
+ "join c2.complaintBullets cb join cb.status s where s.code = ?1 " 
+ "group by c2.id) tmp where c1.id = tmp.id order by tmp.minDate") 

Grazie!

+1

Non si tratta dei dati di primavera, credo che non sia possibile farlo anche in JPQL. –

risposta

6

No, non è possibile avere sottoquery nella clausola select nella query JPQL.

JPQL supporta le sottoquery nelle clausole WHERE e HAVING. Può essere (almeno) parte di qualsiasi, alcuni, tutti, IN, esistono espressioni, e, naturalmente, può essere usato normali espressioni condizionali:

SELECT a 
FROM A a 
WHERE a.val = (SELECT b.someval 
       FROM B b 
       WHERE b.someotherval=3) 
0

Il contenuto del @Query annotazione è più o meno passato come è al provider di persistenza chiamando EntityManager.createQuery(…). Quindi tutto ciò che è permesso in là può essere usato in @Query. AFAIK, JPQL (al momento di JPA 2.0) supporta solo sottoquery per le clausole EXISTS e IN.

+0

JPA 2.0 supporta le subquery nelle clausole WHERE e HAVING. In quelli possono essere utilizzati in molti tipi di espressioni, inclusi EXISTS e IN. –

1

ho ottenuto i risultati attesi in JPA Primavera-dati con

public final static String FIND_BY_ID_STATE = "SELECT a FROM Table1 a RIGHT JOIN a.table2Obj b " + 
               "WHERE b.column = :id" + 
               "AND a.id NOT IN (SELECT c.columnFromA from a.table3Obj c where state = :state)"; 


@Query(FIND_BY_ID_STATE) 
public List<Alert> findXXXXXXXX(@Param("id") Long id, @Param("state") Long state); 

dove

Table2Obj & Table3Obj sono rispettivamente la mappatura del rapporto tra entità Table1 e Table2, Table3.

definito come di seguito.

@OneToMany(mappedBy = "xxx", fetch = FetchType.LAZY) 
private Set<Table2> table2Obj = new HashSet<>(); 
Problemi correlati