Hibernate @ManyToOne mapping:对象引用未保存的瞬态实例

时间:2014-09-12 07:51:13

标签: java hibernate hibernate-mapping many-to-one

我有两个这样的课程:

public class Parent {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "DBId")
    private Long DBId = null;

    @ManyToOne(fetch = FetchType.EAGER)
    @LazyToOne(LazyToOneOption.FALSE)
    private SampleSet samples;
}

public class SampleSet {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "DBId")
    private Long DBId = null;

    private String uniqueId;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "SampleSet_samplesSet")
    private Set<String> samplesSet = new TreeSet<>();
}

我有这样的代码:

public void modifyAndSaveParent(Parent p, Session session) {
    SampleSet s = (SampleSet) session.getNamedQuery("SampleSet.loadByUniqueId").setParameter("uniqueId", p.getSampleSet().getUniqueId()).uniqueResult();
    if (s == null)
        throw new RuntimeException();
    else
        p.setSampleSet(s);

    session.save(p);
    session.flush();
}

问题是session.flush()抛出异常:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: SampleSet
        at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:249)
        at org.hibernate.type.EntityType.getIdentifier(EntityType.java:509)
        at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:309)
        at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:319)
        at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)
        at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4051)
        at org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:535)
        at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:235)
        at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:164)
        at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:228)
        at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:100)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240)

但正如您所见,SampleSet必须已经保留。我该怎么办?

事实上,我不想使用级联。我想手动管理SampleSet,并且不希望对Parent进行级联更改。

3 个答案:

答案 0 :(得分:0)

首先尝试保存SampleSet,然后保存父对象。

session.save(s);

之前添加session.save(p);

答案 1 :(得分:0)

我认为您应首先同步Parent,然后保存它。这样的事情:

p = (Parent) session.load(Parent.class, p.getId());
p.setSampleSet(s);
session.save(p);
session.flush();

答案 2 :(得分:0)

问题不在上面的代码段中。解决了它。试图删除问题但没有成功。 事实上,我在Parent类中有一个List父项。并且其中的SampleSets没有被保留。