我有两个实体Employee
和Department
Employee
实体是我的应用程序的只读数据集(我不拥有此表)
支持我拥有的Department
实体的表数据。如果我在没有注释Employee
到Department
实体关系的情况下查询我的Employee
实体,则查询是超高速的,因为它只产生一个SQL调用。
只要我将Department
关系注释到Employee
实体,就会有大量的SQL调用。以下是我认为每个实体帮助获得帮助的相关细节:
@Entity
Employee
@Id
String employee_number;
String first_name;
String last_name;
String phone_number;
@Column(name="dept")
private String home_dept_number;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="dept", referencedColumnName="dept_number", insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
private Department department;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="dept", referencedColumnName="department_number", insertable = false, updatable = false)
@Entity
Department
@Id
String dept_number
String dept_name;
@OneToOne(fetch = FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name = "department_manager_id", referencedColumnName = "employee_number")
private Employee departmentManager;
@OneToOne(fetch = FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name = "department_admin_aide_id", referencedColumnName = "employee_number")
private Employee departmentAdminAide;
@OneToOne(fetch = FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name = "department_rep_id", referencedColumnName = "employee_number")
private Employee departmentRepresentative;
答案 0 :(得分:3)
我的猜测是缓慢是由@NotFound
注释引起的:Hibernate可能被迫检查部门是否确实存在。
使用@NotFound
可以弥补员工引用不存在的部门的不一致数据库。如果您的数据库是一致的(并且应该由外键约束强制执行),则不需要这些注释。