Hi All,
I'm having troubles when persisting entities tightly related. I have three entities:
ActiveItem
public class ActiveItem extends Entity { @ManyToOne(cascade = {CascadeType.ALL}) @JoinColumn(name = "logicalitemid") private LogicalItem logicalItem;
... }
LogicalItem
public final class LogicalItem extends Entity { @OneToMany(mappedBy = "logicalItem", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) private Set<ActiveItem> activeItems = new HashSet<ActiveItem>();
@OneToOne(fetch = FetchType.LAZY, mappedBy = "logicalItem", cascade = {CascadeType.ALL}) private LatestView latestView;
... }
LatestView
public class LatestView extends View { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "logicalitemid") private LogicalItem logicalItem; @OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST}) @JoinColumn(name = "itemid") private ActiveItem activeItem;
... }
(only the relevant parts are shown here).
My problem is when I save a new activeItem the SQL generated is
insert into logicalitems (ownerid, id) values (?, ?) insert into items_views (policyid, logicalitemid, itemid, id) values (?, ?, ?, ?)
and fails since items_views has a foreign key reference on active item, which does not have an id yet since it wasn't persited... How can I tell Hibernate to save those three entities in reverse topological order?
Thanks! Pascal
|