我正在使用休眠条件创建联接。我的问题是,结果SQL从两个表(1&2)中选择了完整的列集。但是我实际上只需要第一个表,而没有第二个表。
Criteria cr = sessionFactory.getCurrentSession().createCriteria(Publication.class,"p");
// Join on REQUESTS_T, 2nd Table
Criteria requestCriteria = cr.createCriteria("request");
// Restrict by Table 1 column
cr.add(Restrictions.isNotNull("p.someCol"));
// Also restrict by Table 2 columns
ArrayList<Integer> eligibleStatusIds = new ArrayList<Integer>();
eligibleStatusIds.add(19);
eligibleStatusIds.add(20);
requestCriteria.add(Restrictions.in("status.id", eligibleStatusIds));
List<Publication> results = cr.list();
注意SQL:
select
this_.ID as ID1_9_1_,
this_.CREATED_BY_ID as CREATED_2_9_1_,
this_.LAST_CHANGED_BY_ID as LAST_CHA3_9_1_,
// ...etc.
// ...FULL Table 1
request1_.ID as ID1_16_0_,
request1_.TITLE as TITLE3_16_0_,
// ...etc.
// FULL Table 2
from
PUBLICATIONS_T this_
inner join
REQUESTS_T request1_
on this_.REQUEST_ID=request1_.ID
where
this_.SOME_COL is not null
and request1_.STATUS_ID in (
19, 20
)
使用Criteria语法,我需要的是:
select p.* // P Only
from Publications p, Request r
where p.request.id = r.id and ... // restrictions
答案 0 :(得分:1)
我建议您使用(命名的)实体图: jpa entity graphs
通过这种JPA方法,您可以精确定义要从每个查询的实体中获取哪些字段,而无需考虑在实体类中定义的获取策略。然后,您可以按原样保留条件,只需将要获取的字段作为提示传递图形即可