我有两个实体(employeeTo& restTo)有一对多的关系 在他们之间,我想让员工休息只列出,但是当我加载列表时,每个对象都包含员工对象。
employeeTO
@OneToMany(mappedBy = "employeeTO",cascade = CascadeType.ALL)
private List<RestTO> RestTOList=new ArrayList<>();
restTO
@ManyToOne(fetch = FetchType.LAZY)
private EmployeeTO employeeTO;
你可以在下面看到我的代码:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria=session.createCriteria(RestTo.class);
criteria.setFetchMode("employeeTO",FetchMode.SELECT);
criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("alians_employee.emID",emid);
criteria.add(condition1);
criteria.add(Restrictions.between("startDate",date1,date2));
List<RestTO> restTOS=criteria.list();
transaction.commit();
session.close();
的System.out.println(restTOS.get(0).getEmployeeTo.getName); 该指令正在运行并显示员工姓名
我希望无权访问员工对象
我只需加载没有employeeTo值的RestTo对象。 我怎么能这样做? 感谢。
答案 0 :(得分:0)
不要创建别名,它会为您的查询添加内部联接。所以不是这个
Criteria criteria=session.createCriteria(RestTo.class);
criteria.setFetchMode("employeeTO",FetchMode.SELECT);
criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("alians_employee.emID",emid);
使用此
Criteria criteria=session.createCriteria(RestTo.class);
//Default fetchmode is good,no need for that.
//criteria.setFetchMode("employeeTO",FetchMode.SELECT);
//criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("employeeTO.emID",emid);