我的父班:
@Entity
public class FooParent
{
@OneToMany(fetch=FetchType.LAZY,orphanRemoval=true,mappedBy="parent")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public Map<FooChild,FooChild> kids = new HashMap<>();
@Id @GeneratedValue(strategy=GenerationType.TABLE)
Integer id;
public FooParent ()
{
}
}
和儿童班:
@Entity
public class FooChild
{
@Id @GeneratedValue(strategy=GenerationType.TABLE)
Integer id;
@ManyToOne
FooParent parent;
public FooChild (FooParent parent)
{
this.parent=parent;
}
}
我发现级联持久性似乎只适用于Map的值,而不是键。如果我这样做:
FooParent foo = new FooParent();
FooChild kid1 = new FooChild(foo);
FooChild kid2 = new FooChild(foo);
foo.kids.put(kid1,kid2);
hibSession.persist(foo);
我明白了:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.cards.FooChild
如果我在kid1
之前坚持()foo
,那么一切正常。另外,如果我将kids
更改为Map<Integer,FooChild>
,则其工作方式与List
这是一个限制,还是我需要做的其他事情才能级联映射的Map键的持久性?