我有Hibernate(和Ehcache插件)的二级缓存问题。在数据库中,我声明了一个parent_id
字段,它是同一个表的外键(最大树深度只有2)。
所以实体看起来像这样:
@Entity(name="Foo")
@Table(name="foo")
public class Foo implements Comparable<Foo> {
@Id
@Column(name="foo_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="foo_bar_id")
@ManyToOne
@JoinColumn(name="foo_bar_id", referencedColumnName="bar_id")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Bar bar;
@ManyToOne
@JoinColumn(name = "foo_parent_id", referencedColumnName = "foo_id")
private Foo parentFoo;
@OneToMany(fetch = FetchType.EAGER, mappedBy="parentFoo")
@OrderBy(value = "foo_id")
private SortedSet<Foo> childFoos = new TreeSet<>();
public Foo() {}
public Foo(int id, Bar bar, Foo parentFoo, SortedSet<Foo> childFoos) {
this.id = id;
this.bar = bar;
this.parentFoo = parentFoo;
this.childFoos = childFoos;
}
public int compareTo(Foo foo) {
// ...
}
// ... getters and setters here ...
}
@Entity(name="Bar")
@Table(name="bar")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Bar implements Comparable<Bar> {
// ...
}
因此Foo
不在二级缓存中,但是Bar
。问题是当我检索具有Foo
父级的Foo
实体时,不会从缓存中检索父级的Bar
实体。
另外,我做了一些测试:
Foo
标记为可缓存,则一切正常。LAZY
字段上设置EAGER
而非childFoos
,则在延迟加载实体时不会更改任何内容。我在这里缺少什么?
Hibernate版本:4.3.2.Final
由于