i have two entity(employeeTo & restTo) there is one-to-many relationship between them,i want to get employee restTo list only, but when i load the list each of object contain employee object.
employeeTO
@OneToMany(mappedBy = "employeeTO",cascade = CascadeType.ALL)
private List<RestTO> RestTOList=new ArrayList<>();
restTO
@ManyToOne(fetch = FetchType.LAZY)
private EmployeeTO employeeTO;
and you can see my code below:
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); This instruction is working and show employee name
I want to not have access to employee object
i need load only RestTo object without employeeTo value. how i can do it? thanks.
Don't create an alias,it adss an inner join to your query. So instead of this
Criteria criteria=session.createCriteria(RestTo.class);
criteria.setFetchMode("employeeTO",FetchMode.SELECT);
criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("alians_employee.emID",emid);
Use this
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);