我们在Hibernate中有一个需求,那就是假设我们有一个类名Employee,它包含Departments列表。但这里Department不包含任何字段,其中只包含一个包含部门字段的地图。所以请告诉我Employee和Department类的Hibernate配置。Hibernate中的地图配置
感谢, Narsi p
我们在Hibernate中有一个需求,那就是假设我们有一个类名Employee,它包含Departments列表。但这里Department不包含任何字段,其中只包含一个包含部门字段的地图。所以请告诉我Employee和Department类的Hibernate配置。Hibernate中的地图配置
感谢, Narsi p
也许你正在谈论单向一个一对多的关系? 在这种情况下,你应该使用
@Entity
public class Employee {
@OneToMany
private Set<Department> departments;
public Set<Department> getDepartments() {
return departments;
}
public void setDepartments(Set<Department> departments)
{
this.departments = departments;
}
//....
}
@Entity
public class Department {
// No link to Employee
@Id
private long id;
private String name;
//....
}
在这种情况下,你从员工甲肝参考到部,但没有从部门参考至员工。
UPD1:
如果要映射基本类型,你可以使用@ElementCollection如被narsi提到:
@ElementCollection
Map<String, String> departments;
在Employee类,我像映射@ElementCollection 私人清单 deptList; –
narsi
请编辑您的问题,并提供一些示例代码 –