2 xsh744497 xsh744497 于 2014.09.29 18:47 提问

Spring data JPA使用CriteriaQuery进行不定条件查询时,发起了多次查询

//PlatformSecurityUser为账户信息实体,和用户基本信息platformSysUser存在一对一的映射关系,fetchType为lazy

//在获取账户信息的同时,希望一次获取用户基本信息-platformSysUser
import javax.persistence.*;
import javax.persistence.criteria.*;
import java.util.ArrayList;
public class CustomPlatformSecurityUserDaoImpl implements CustomPlatformSecurityUserDao {

@PersistenceContext
EntityManager entityManager;
@Override
public Page<PlatformSecurityUser> dynamicQuery(PlatformSysUserAccountQueryConditionVO queryConditionVO, Pageable pageable) {
    CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
    CriteriaQuery<PlatformSecurityUser> query=criteriaBuilder.createQuery(PlatformSecurityUser.class);
    Root< PlatformSecurityUser> root = query.from(PlatformSecurityUser.class);
    root.fetch("platformSysUser");
    entityManager.createQuery(query).getResultList();
    return null;
}

}
现在出现的问题是resultList里面有多少账户信息数据,就发起了多少次根据ID获取账户信息的查询,现在我希望只有一次查询,就是日志中的第一条SQL,请各位哥哥姐姐帮下忙。打印的日志如下(sql我进行过简化,把 里面的AS去掉了):
Hibernate: select a.id, c.id, a.IS_DEL, a.CREATE_TIME, a.EFF_TIME, a.EXP_TIME, a.LAST_LOGIN_TIME, a.LOGIN_NAME, a.PASSWORD, a.PLATFORM_SYS_USER_ID, a.STATUS, a.TOKEN, c.IS_DEL, c.CREDENTIAL_NUMBER, c.CREDENTIAL_TYPE, c.EMAIL, c.MOBILE, c.NAME, c.SEX, c.USER_NUMBER, c.USER_TYPE from PLATFORM_SECURITY_USER a inner join PLATFORM_SYS_USER b on a.PLATFORM_SYS_USER_ID=b.id inner join PLATFORM_SYS_USER c on a.PLATFORM_SYS_USER_ID=c.id
Hibernate: select a.id, a.IS_DEL, a.CREATE_TIME, a.EFF_TIME, a.EXP_TIME, a.LAST_LOGIN_TIME, a.LOGIN_NAME, a.PASSWORD, a.PLATFORM_SYS_USER_ID, a.STATUS, a.TOKEN from PLATFORM_SECURITY_USER a where a.PLATFORM_SYS_USER_ID=?
Hibernate: select a.id, a.IS_DEL, a.CREATE_TIME, a.EFF_TIME, a.EXP_TIME, a.LAST_LOGIN_TIME, a.LOGIN_NAME, a.PASSWORD, a.PLATFORM_SYS_USER_ID, a.STATUS, a.TOKEN from PLATFORM_SECURITY_USER a where a.PLATFORM_SYS_USER_ID=?
Hibernate: select a.id, a.IS_DEL, a.CREATE_TIME, a.EFF_TIME, a.EXP_TIME, a.LAST_LOGIN_TIME, a.LOGIN_NAME, a.PASSWORD, a.PLATFORM_SYS_USER_ID, a.STATUS, a.TOKEN from PLATFORM_SECURITY_USER a where a.PLATFORM_SYS_USER_ID=?
Hibernate: select a.id, a.IS_DEL, a.CREATE_TIME, a.EFF_TIME, a.EXP_TIME, a.LAST_LOGIN_TIME, a.LOGIN_NAME, a.PASSWORD, a.PLATFORM_SYS_USER_ID, a.STATUS, a.TOKEN from PLATFORM_SECURITY_USER a where a.PLATFORM_SYS_USER_ID=?

Csdn user default icon
上传中...
上传图片
插入图片