JPA-Hibernate Lazy Fetching -
i trying understand behaviour of jpa-hibernate in below situation.
i have 2 entities, department , employee have 1 many relationship. trying implement using jpa-hibernate.
here department entity:
@entity @table(name="department") public class department { @id @generatedvalue @column(name="department_id") private long departmentid; @column(name="dept_name") private string departmentname; @onetomany(mappedby="department") private set<employee> employees; }
here employee entity:
@entity @table(name="employee") public class employee { @id @generatedvalue @column(name="employee_id") private long employeeid; @column(name="firstname") private string firstname; @column(name="lastname") private string lastname; @manytoone(fetch = fetchtype.lazy) @joincolumn(name="department_id") private department department; public employee() { } public employee(string firstname, string lastname, string phone) { this.firstname = firstname; this.lastname = lastname; this.birthdate = new date(system.currenttimemillis()); this.cellphone = phone; } }
while fetching employee dont want fetch department. use fetchtype.lazy. suppose few employees associated 2 departments , in such case when fetch employee, exception department has multiple records. since use fetchtype.lazy, assumption department should not fetched , hence no matter if employee associated 1 department or 2 department, should able fetch employee.
can me understand why exception?
thanks.
Comments
Post a Comment