I want to create a criteria with a custom POJO
properties:
UserPojo
Code:
private Long id;
private String name;
private Long localeId;
private List<GroupPojo> groups;
GroupPojo
Code:
private Long id;
private String name;
my criteria code is:
Code:
final CriteriaBuilder criteria = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<IGesplanEntity<?>> criteriaQuery = (CriteriaQuery<UserPojo>) criteria.createQuery(UserPojo.class);
final Root<T> root = criteriaQuery.from(User.class);
List<Selection<?>> selections = new ArrayList<>();
Field[] iFields = UserPojo.class.getDeclaredFields();
for (Field field : iFields) {
Path<Object> fieldPath = root.get(field.getName());
if (Collection.class.isAssignableFrom(field.getType())) {
Join<Object, Object> join = root.join(field.getName());
Field[] iFields1 = clazz.getDeclaredFields();
for (Field field2 : iFields1) {
selections.add(join.get(field2.getName()));
}
} else {
selections.add(fieldPath);
}
}
Selection<? extends IGesplanEntity<?>> select = (Selection<? extends IGesplanEntity<?>>) ((CriteriaBuilderImpl) criteria).construct(entity.getClass(), selections);
criteriaQuery.select(select);
final TypedQuery<IGesplanEntity<?>> query = this.getEntityManager().createQuery(criteriaQuery);
query.getResultList()
but i got a problem, when it will do the select for my GroupPojo it dont add new GroupPojo in query.
i want the the ouput is
Code:
select new UserPojo(u.id, u.name, u.localeId, new list(new GroupPojo(g.id, g.name)) from User as u inner join u.group as g
but i dont know how to made it with criteria, can anyone help?
i got almost
Code:
select new UserPojo(u.id, u.name, u.localeId) from User as u inner join u.group as g
(using where condition)
i could get too
Code:
select new UserPojo(u.id, u.name, u.localeId, g.id, g.name) from User as u inner join u.group as g
(but it is not a list)
but i need the list of my GroupPojo. I search in google and forum but not found a solution.