我有以下 2 个类 Employee 和 Phonenumber,员工和电话号码之间存在单对关系: Employee:
@Entity
@Table(name="employee")
public class Employee
@Id
@GeneratedValue
private Integer id;
@OneToMany(fetch = FetchType.LAZY, mappedBy="employee_id")
@JoinColumn(name = "ID")
private Set<PhoneNumber> phonenumbers = new HashSet<PhoneNumber>(0);
public Set<PhoneNumber> getPhonenumbers() {
return phonenumbers;
}
public void setPhoneNumbers(Set<PhoneNumber> phonenumbers) {
this.phonenumbers = phonenumbers;
}
电话号码 @Entity @Table(name="phone_number") public class PhoneNumber {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employeeid;
public Employee getEmployeeid() {
return this.employeeid;
}
public void setEmployeeid(Employee employeeid) {
this.employeeid = employeeid;
}
1) employee
columns:
id
name
2) phone_number
columns:
employee_id(FK)
phone_number_type
phone_number
我想执行一个查询,我可以在其中搜索员工可能拥有的电话号码并返回所有数据。基本上,如果我搜索电话号码,我想获取该员工以及该员工的所有电话号码。如果由于某种原因 2 名员工共用一部电话,我想返回这两名员工的所有信息。我使用了以下示例查询,但它没有返回女员工拥有的所有电话号码。
String key = new String("phone_number_type");
String value = new String("phone_number");
Criteria criteria = getCurrentSession().createCriteria(Employee.class)
.add(Restrictions.eq(key, search.getPhonenNumbertype().toString()))
.add(Restrictions.eq(value, search.getPhoneNumber()));
return (List<Employee>) criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
我需要创建自定义 HQL 来执行此操作吗?