更具体地说,(map<Course, Role>)((User) u).courses
我有很多Course
和很多在课程中有Role
的User
。用户可能是某门课程的教员,而另一门课程则是学生。
我有ID为User entities、ID为course entities的User entities和Role Enum类型
我想要一个属于user的映射,以便查询他在特定类中的角色:
public enum Role{ STUDENT, STAFF }
public class User ... {
...
@ElementCollection(targetClass=Role.class)
@CollectionTable(name="COURSE_ROLE")
@MapKeyJoinColumn(name="COURSE_ID")
@Column(name="ROLE_NAME")
@Enumerated(EnumType.STRING)
private Map<Course, Role> courses;
...
我已经查看了几个资源。ProJPA具有以下特性:
@Entity
public class Department {
@Id private int id;
private String name;
// ...
@ElementCollection
@CollectionTable(name="EMP_SENIORITY")
@MapKeyJoinColumn(name="EMP_ID")
@Column(name="SENIORITY")
private Map<Employee, Integer> seniorities;
// ...
如果我们把一个部门看作我的用户,把employee看作我的课程,这几乎是可行的,但我不知道如何处理从他们的Integer到我的Enum的转换。
请帮助我了解这个带有注释的表定义的语义。
编辑:为了澄清,我已经尝试了没有ElementCollection(Role.class)的争论,以及许多其他选项的排列,但我觉得我没有掌握这种情况。目前,对于这个注释组合,我得到了一个异常:
org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: USER,
for columns: [org.hibernate.mapping.Column(ROLE_NAME)]
发布于 2012-01-07 06:32:07
我只需要更新我的maven依赖hibernate版本。
https://stackoverflow.com/questions/8751872
复制相似问题