Вопрос

Hi i'm struggling to write the following query with criteria api: "SELECT c.id,curr.name FROM Cargo c, Currency curr"

The problem is that both Cargo and Currency are aliased as c by criteria so the resulting jpql becomes "SELECT c.id,c.name FROM Cargo c, Currency c".

I don't know if it is because both entities start with C. Is there a way to chage the table alias?

CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> query= criteriaBuilder.createTupleQuery();

Root<Cargo> cargo= query.from(Cargo.class);
Root<Currency> currency= query.from(Currency.class);

Any help is very much appreciated.

Это было полезно?

Решение

Your code is missing the part where you specify what you want selected. "SELECT c.id,curr.name FROM Cargo c, Currency curr" would likely translate to something like:

CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> query= criteriaBuilder.createTupleQuery();

Root<Cargo> cargo= query.from(Cargo.class);
Root<Currency> currency= query.from(Currency.class);

query.multiselect(cargo.get("id"), currency.get("name"));
Query query = em.createQuery(cq);
List<Tuple> results = query.getResultList();

There is an example here using only one table: http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria#Tuple_Queries

Другие советы

After lots of debugging, i found out that the actual jpql that is executed is different from the one that is displayed in my ide(eclipse). The aliases were ok.I think i was a bit deceived by the ide. Thanks all for your attention.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top