2012-09-14 21 views
10

Mam wiele do wielu relacji między dwoma podmiotami o nazwie: Car and Dealership.JPQL ManyToMany wybierz

W rodzimej MySQL mam:

car (id and other values) 
dealership (id and other values) 
car_dealership (car_id and dealership_id) 

a zapytanie Chcę, aby w JPQL jest:

#Select List of cars in multiple dealerships 
SELECT car_id FROM car_dealership WHERE dealership_id IN(1,2,3,56,78,999); 

Co to jest właściwy sposób, aby równowartość JPQL?

metoda podpis

My Java jest:

public List<Car> findByDealership(List<Dealership> dealerships); 

Próbowałem

//TOTALLY WRONG QUERY CALL!!!  
    Query query = em.createQuery("SELECT c FROM Car c WHERE :dealer_ids IN c.dealerships"); 
    List<Long> dealerIds = new ArrayList<Long>(); 
    for(Dealership d : dealerships) { 
     dealerIds.add(d.getId()); 
    } 
    query.setParameter(":dealer_ids", dealerIds); 
    List<Dealership> result = (List<Dealership>) query.getResultList(); 
    return result; 
} 

Oto moja JPA Adnotacje dla takiej relacji w Java:

@Entity 
@Table(name = "car") 
public class Car implements Serializable { 

    //Setup of values and whatnot.... 
    @ManyToMany 
    @JoinTable(name = "car_dealership", joinColumns = 
    @JoinColumn(name = "car_id", referencedColumnName = "id"), 
    inverseJoinColumns = @JoinColumn(name = "dealership_id", referencedColumnName = "id")) 
    private List<Dealership> dealerships; 

    ... other stuff (getters/setters) 

} 

@Entity 
@Table(name = "property") 
public class Dealership implements Serializable { 

    //Setting of values and whatnot 

    @ManyToMany(mappedBy = "dealerships") 
    private List<Car> cars; 

    .... other stuff(getters/setters) 

} 

EDIT

ja też próbowałem:

Query query = em.createQuery("SELECT c FROM Car c INNER JOIN c.dealerships d WHERE d IN (:deals)"); 
query.setParameter("deals", dealerships); 

Który rzucił błąd:

org.eclipse.persistence.exceptions.QueryException 

Exception Description: Object comparisons can only use the equal() or notEqual() operators. 
Other comparisons must be done through query keys or direct attribute level comparisons. 

Expression: [ 
Relation operator [ IN ] 
Query Key dealerships 
    Base stackoverflow.question.Car 
Constant [ 
Parameter deals]] 

Odpowiedz

25
select car from Car car 
inner join car.dealerships dealership 
where dealership in :dealerships 

Parametr musi być zbiorem przypadków ASO.

Jeśli chcesz użyć zbiór identyfikatorów dealerskich, użyj

select car from Car car 
inner join car.dealerships dealership 
where dealership.id in :dealershipIds 

Remamber że JPQL zawsze używać jednostek, odwzorowane atrybuty i skojarzenia. Nigdy nie używaj nazw tabel i kolumn.

+0

dziękuję, zobacz moją edycję. – gtgaxiola

+0

Wygląda na to, że EclipseLink nie chce porównywać obiektów. Będzie działać z Hibernate. Skorzystaj z mojej drugiej sugestii, która porównuje identyfikatory. –

+0

Dzięki ... właśnie jako notatka EclipseLink nie lubi nawiasów w części IN (: dealershipIds) ... – gtgaxiola