Hi all,
I am new to Hibernate, I have a couple probably relatively simple questions :
I have two entities, Department and Employee, representing two db tables, connected in the one-to-many relation (Employee db table, as usual, have foreign key pointing to the Department table) :
Code:
@Entity
@Table(name = "DEPARMENT", uniqueConstraints = {...})
public class Department implements java.io.Serializable {
private Set<Employee> employees = new HashSet<Employee>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "...")
public Set<Employee> getEmployees() {
return this.employees;
}
public void setEmployees()(Set<Employees> employees) {
this.employees = employees;
}
}
@Entity
@Table(name = "EMPLOYEE", uniqueConstraints = {...})
public class Employee implements java.io.Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPARTMENT_ID", nullable = false)
public TeeDrzava getDepartment() {
return this.department;
}
}
- So, nothing special. As you can see, for a particular Department instance, I can get all employees in that department by calling department.getEmployees(). Now, what I want, is :
1. Determine total number of employees for that department. Can I simply use
Code:
department.getEmployees().size()
/and what hibernate will do in this case - pre-load all employees into memory ?/ or exist a better (cheaper) way to do this ?
2. I want to 'paging' throughout employees obtained with department.getEmployees(); in the same way as creating Query an setting first result
/setFirstResult();/ and setMaxResults(). So, starting from the n-th object, I want to retrieve the next m objects. How to do that ?
3. I want to further refine search throughout department.getEmployees() - let user to propose search criteria, and to find all Employees (for particular department) conforming that search criteria. How to do this ? Should I make Query (
"from Employee e where e.department = ? and e.name like ?" ) or I can achieve this somehow with Java code and maybe annotation ?
- thanks in advance,...