I'm trying to perform a simple One to Many association with Fetch Mode SELECT. In my case, there is an EMPLOYEE table that has been mapped to EMPLOYEE_EXPENSE with One to Many association.
Here is the snippet Employee Class @Fetch(FetchMode.SELECT) @OneToMany(mappedBy="employee", fetch=FetchType.LAZY) private Set<EmployeeExpense> employeeExpenses;
Employee Expense Class @Id @ManyToOne @JoinColumn(name="EMP_ID") private Employee employee;
Im doing this below code to fetch and display the values tx = session.beginTransaction(); emp = (Employee) session.get(Employee.class, empId); log.info("Employee Expense size=" + emp.getEmployeeExpenses().size()); for (EmployeeExpense ee: emp.getEmployeeExpenses()) { log.info("expense claim=" + ee.getExpenseClaim()); } session.getTransaction().commit();
For the empId = 7369, i see many records in EmployeeExpense table. But what gets printed from the above statements is just one record.. [main] [class: EmployeeDAOImpl] INFO - Employee Expense size=1 [main] [class: EmployeeDAOImpl] INFO - expense claim=3072.43
I see hibernate is executing one SELECT statement to fetch the Employee 7369 and then its making another SELECT statement to fetch the associated EMPLOYEE EXPENSE records. Eventhough there are many EMPLOYEE EXPENSE records in the database for the EMPID 7369, what gets fetched is JUST ONE EmployeeExpense..
Whats happening here? Could anyone help me out? Thanks!
|