JPA / Hibernate:CriteriaBuilder - 如何使用关系对象创建查询? [英] JPA / Hibernate: CriteriaBuilder - How to create query using relationship object?
问题描述
SCHEDULE_REQUEST表:
ID,
APPLICATION_ID(FK)
应用表:
ID。
USER_APPLICATION TABLE: APPLICATION_ID(FK),
USER_ID(FK)
用户表:
ID,
NAME
想要创建Criteria构建器,其中条件是为指定的用户ID选择ScheduleRequests。
我有以下代码:
List< User> usersList = getSelectedUsers(); // userList包含我想要选择的用户
CriteriaBuilder builder = getJpaTemplate()。getEntityManagerFactory()。getCriteriaBuilder();
CriteriaQuery< ScheduleRequest> criteria = builder.createQuery(ScheduleRequest.class);
Root< ScheduleRequest> scheduleRequest = criteria.from(ScheduleRequest.class);
criteria = criteria.select(scheduleRequest);
ParameterExpression< User> usersIdsParam = null;
if(usersList!= null){
usersIdsParam = builder.parameter(User.class);
params.add(builder.equal(scheduleRequest.get(application.userApplications.user),usersIdsParam));
}
criteria = criteria.where(params.toArray(new Predicate [0]));
TypedQuery< ScheduleRequest> query = getJpaTemplate()。getEntityManagerFactory()。createEntityManager()。createQuery(criteria);
//编译时间错误在这里:
// TypedQuery< ScheduleRequest>类型中的方法setParameter(Parameter< T> ;, T) (ParameterExpression< User> ;, List< User>)
query.setParameter(usersIdsParam,usersList);
返回query.getResultList();
你能帮我一下如何将查询过滤器传递给关系对象吗?
我认为我在application.userApplications.user中所做的是错误的?
请真的需要帮助。
预先感谢您。
使用规范Metamodel和一些连接,它应该可以工作。试试如果你从下面的伪代码中得到一些提示(未经测试):
$ $ $ $ $ $ $ $ $ $ $ = cb.disjunction();
if(usersList!= null){
ListJoin< ScheduleRequest,Application>应用程序= scheduleRequest.join(ScheduleRequest_.applications);
ListJoin< Application,UserApplication> userApplications = applications.join(Application_.userApplications);
加入< UserApplication,User> user = userApplications.join(UserApplication_.userId); (字符串用户名:usersList){
谓词= builder.or(谓词,builder.equal(user.get(User_.name),用户名));
。
}
}
criteria.where(predicate);
...
为了理解Criteria Queries,请看这些教程:
http://www.ibm.com/developerworks/java/ library / j-typesafejpa /
http:/ /docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
第二个链接还应指导您如何使用元模型类,这应该由编译器/ IDE自动构建。
I have the following four tables:
SCHEDULE_REQUEST TABLE: ID, APPLICATION_ID (FK)
APPLICATION TABLE: ID. CODE
USER_APPLICATION TABLE: APPLICATION_ID (FK), USER_ID (FK)
USER TABLE: ID, NAME
Now I wanted to create Criteria builder where condition is to select ScheduleRequests for specified user Ids.
I have the following codes:
List<User> usersList = getSelectedUsers(); // userList contains users I wanted to select
CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder();
CriteriaQuery<ScheduleRequest> criteria = builder.createQuery(ScheduleRequest.class);
Root<ScheduleRequest> scheduleRequest = criteria.from(ScheduleRequest.class);
criteria = criteria.select(scheduleRequest);
ParameterExpression<User> usersIdsParam = null;
if (usersList != null) {
usersIdsParam = builder.parameter(User.class);
params.add(builder.equal(scheduleRequest.get("application.userApplications.user"), usersIdsParam));
}
criteria = criteria.where(params.toArray(new Predicate[0]));
TypedQuery<ScheduleRequest> query = getJpaTemplate().getEntityManagerFactory().createEntityManager().createQuery(criteria);
// Compile Time Error here:
// The method setParameter(Parameter<T>, T) in the type TypedQuery<ScheduleRequest> is not // applicable for the arguments (ParameterExpression<User>, List<User>)
query.setParameter(usersIdsParam, usersList);
return query.getResultList();
Can you please help me how to pass query filter to a relationship object? I think what I did in "application.userApplications.user" is wrong? Please really need help. Thank you in advance.
Using the canonical Metamodel and a couple of joins, it should work. Try if you get some hints from the following pseudo-code (not tested):
...
Predicate predicate = cb.disjunction();
if (usersList != null) {
ListJoin<ScheduleRequest, Application> applications = scheduleRequest.join(ScheduleRequest_.applications);
ListJoin<Application, UserApplication> userApplications = applications.join(Application_.userApplications);
Join<UserApplication, User> user = userApplications.join(UserApplication_.userId);
for (String userName : usersList) {
predicate = builder.or(predicate, builder.equal(user.get(User_.name), userName));
}
}
criteria.where(predicate);
...
In order to understand Criteria Queries, have a look at these tutorials: http://www.ibm.com/developerworks/java/library/j-typesafejpa/ http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
The second link should also guide you on how to use Metamodel classes, that should be built automatically by the compiler / IDE.
这篇关于JPA / Hibernate:CriteriaBuilder - 如何使用关系对象创建查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!