【发布时间】:2019-03-27 11:31:47
【问题描述】:
我最近在我们的 repo 中发现了这样的映射:
@Entity
...
public class Foo{
...
@OneToMany(mappedBy = "someField", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Bar> bars;
...
}
@Entity
@IdClass(Bar.Key.class)
...
public class Bar{
@Id
public long someField;
@Id
public long anotherField;
...
static class Key implements Serializable {
public long someField;
public long anotherField;
}
}
两个表中都没有外键。
我很惊讶Bar 类中没有@ManyToOne 字段,并且@OneToMany 在没有@JoinColumn 的情况下被注释为mappedBy。无论如何,它工作得很好:没有多余的更新——就像它是一个双向映射。我不是 JPA/Hibernate 方面的专家,而且我从未在教程/指南/文档中看到过这样的映射。我试图用谷歌搜索这样的映射,但没有找到任何解释。可以像这样映射实体吗?
谢谢!
【问题讨论】: