Set selection of typesafe JPA 2 query with joins

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 05:06:54

问题


I have simple query with two joins:

SELECT p.id, BU.ID, BU.TEXTLANG  as Title FROM PROJEKT P
  JOIN Text BT ON BT.ID = P.TITEL_ID
  JOIN Uebersetzung BU ON BU.TEXT_ID = BT.ID
  WHERE BU.TEXTLANG LIKE '%Spatial%';

This query must be converted to a typesafe query, what i tried is:

final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Projekt> query = criteriaBuilder.createQuery(Projekt.class);
final Root<Projekt> projekt = query.from(Projekt.class);
Join<Projekt, Text> bt = projekt.join(Projekt_.titel);
Join<Text, Uebersetzung> bu = bt.join(Text_.uebersetzungen);
Predicate condition = criteriaBuilder.like(bu.get(Uebersetzung_.textLang),"%Spatial%");
query.where(condition);
query.distinct(true);
query.orderBy(criteriaBuilder.asc(projekt.get(Projekt_.id)));

My problem is, that i want to get the field Uebersetzung_.textLang as selection. But when i try to add selection to the query, for example:

query.multiselect(bu.get(Uebersetzung_.textLang));

i get an error message:

org.apache.openjpa.persistence.OptimisticLockException: Unable to obtain an object lock on "null".

I need this field, because i want to access it from the jsp page. Is there any other way to get this field in the selection?

来源:https://stackoverflow.com/questions/6660819/set-selection-of-typesafe-jpa-2-query-with-joins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!