【发布时间】:2014-01-12 14:37:42
【问题描述】:
如何注释一个映射,其中键是实体类,值是实体类中的普通 java 对象(在我的例子中是布尔值)?
我有两个@Entity 类:Voter 和 Poll。 在 Poll 类中,我想保留可以对此投票进行投票的 Voter 的 Map。布尔值标记选民是否投票。因此,在对选民的民意调查中,映射是多对多的。
我有课:
@Enity
public class Voter {
...some attributes and their getters and setters
private List<Poll> polls;
private int voterId;
@Id
public int getVoterId() {
return voterId;
}
@ManyToMany(mappedBy="voters")
public List<Poll> getPolls() {
return polls;
}
..and setter.
}
@Enity
public class Poll {
...some attributes and their getters and setters
private Map<Voter,Boolean> voters;
@ManyToMany
@JoinColumn(referencedColumnName="voterId")
public Map<Voter,Boolean> getVoters() {
return voters;
}
..and setter.
}
运行时失败并导致 AnnotationException。 我已经看到使用了注释@MapKeyJoinColumn,并尝试使用它(代替@JoinColumn),但失败了。我还没有在任何地方找到这样的例子(映射实体的键,映射的值只是一个对象),所以我基本上使用了尝试失败的方式。
所以问题是:我应该在哪里放什么注释?
【问题讨论】:
-
我猜,您遇到的一个问题是您在 Voter 类中的 @ManyToMany 注释将字段 Poll.voters 称为该关联的另一端,这不可能是真的,因为 voters 是不是选民类型的字段。将您的地图重命名为另一个名称,应该修复此错误。但是,这并不能解决您的地图问题。
标签: java hibernate map boolean