我在數據庫中有一個簡單的一對多關係。這裏有相應的職業:使用Hibernate更新表中'多個'項目的計數
class Department
{
int deptId;
int count; //This corresponds to the total number of Employees in a department
List<Employee> employees; //An employee belongs to only one department
}
class Employee
{
int employeeId;
Department dept;
}
爲了保持count
更新,我在每個CUD
操作設定count
。有沒有一種自動方式可以將員工每次添加/刪除時更新count
? (註釋/限制什麼的?)
不錯的答案。你爲什麼只冷靜在stackoverflow上? :P – 2013-02-28 10:20:10
這只是我不想加載每個部門訪問中的所有員工行。 FetchType是LAZY。但是,我需要顯示每個部門閱讀的摘要(員工人數)。 – 2013-02-28 10:34:50
然後,您應該看看「Extra Lazy」提取startegy:http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#performance-fetching。如果你使用註釋,你只需要添加@LazyCollection(LazyCollectionOption.EXTRA) – overmeulen 2013-03-01 08:35:25