2015-09-07 34 views
-1

我们在Hibernate中有一个需求,那就是假设我们有一个类名Employee,它包含Departments列表。但这里Department不包含任何字段,其中只包含一个包含部门字段的地图。所以请告诉我Employee和Department类的Hibernate配置。Hibernate中的地图配置

感谢, Narsi p

+0

在Employee类,我像映射@ElementCollection 私人清单 deptList; – narsi

+0

请编辑您的问题,并提供一些示例代码 –

回答

0

也许你正在谈论单向一个一对多的关系? 在这种情况下,你应该使用

@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; 
+0

是Amuza,仅此类似,但在Department中,我们没有id和name属性,我们只有Map 。那么我们如何在这种情况下配置Department? – narsi

+0

试试@ElementCollection,我已经更新了我的答案。 – akamuza