TransientObjectException:对象引用未保存的瞬态实例 - 在进行合并时,在刷新之前保存瞬态实例

时间:2016-06-08 08:27:38

标签: java hibernate

任何人都可以解释我为什么在进行合并时得到TransientObjectException。只有当我在ActivityTestEntity的构造函数中创建RuleTestEntitiy时才会重现问题,如下所示。如果我更新或创建,它不会出现。

提前致谢。

这是一个测试:

@ContextConfiguration(locations = {
    "classpath:testApplicationContext_db.xml"})
public class TransientObjectExceptionTest extends  AbstractTestNGSpringContextTests{
@Autowired
SessionFactory sessionFactory;
@Test
public void testAddTestActivity(){
    Session session = sessionFactory.openSession();
    Transaction tx1 = session.beginTransaction();
    ActivityTestEntity newActivityEntity = new ActivityTestEntity();
    session.merge(newActivityEntity);
    tx1.commit();
    session.close();
    sessionFactory.close();
}

}

@ContextConfiguration(locations = { "classpath:testApplicationContext_db.xml"}) public class TransientObjectExceptionTest extends AbstractTestNGSpringContextTests{ @Autowired SessionFactory sessionFactory; @Test public void testAddTestActivity(){ Session session = sessionFactory.openSession(); Transaction tx1 = session.beginTransaction(); ActivityTestEntity newActivityEntity = new ActivityTestEntity(); session.merge(newActivityEntity); tx1.commit(); session.close(); sessionFactory.close(); } 例外: org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.xxx.RuleTestEntity

@Entity
@Table(name = "ACTIVITY_TEST")
public class ActivityTestEntity implements Serializable{
private static final long serialVersionUID = 4190826330152288861L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ACTIVITY_ID", nullable = false)
private long id;
@OneToMany(mappedBy = "activity", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<RuleTestEntity> rules = new HashSet<>();
public ActivityTestEntity() {
    RuleTestEntity rule = new RuleTestEntity();
    rule.setActivity(this);
    this.getRules().add(rule);
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public Set<RuleTestEntity> getRules() {
    return rules;
}
public void setRules(Set<RuleTestEntity> rules) {
    this.rules = rules;
}

}

@Entity @Table(name = "ACTIVITY_TEST") public class ActivityTestEntity implements Serializable{ private static final long serialVersionUID = 4190826330152288861L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ACTIVITY_ID", nullable = false) private long id; @OneToMany(mappedBy = "activity", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) private Set<RuleTestEntity> rules = new HashSet<>(); public ActivityTestEntity() { RuleTestEntity rule = new RuleTestEntity(); rule.setActivity(this); this.getRules().add(rule); } public long getId() { return id; } public void setId(long id) { this.id = id; } public Set<RuleTestEntity> getRules() { return rules; } public void setRules(Set<RuleTestEntity> rules) { this.rules = rules; }

1 个答案:

答案 0 :(得分:0)

当您尝试保存引用未保存实体的实体时,会生成此异常。您可以先保存引用的实体,然后保存实体,也可以将cascade=CascadeType.All添加到它们之间的关系中。

删除构造函数中的代码,然后尝试以下代码:

    Session session = sessionFactory.openSession();
    Transaction tx1 = session.beginTransaction();
    RuleTestEntity rule = new RuleTestEntity();
    session.save(rule);
    session.refresh(rule);
    ActivityTestEntity newActivityEntity = new ActivityTestEntity();
    Set<RuleTestEntity> rules = new HashSet<>();
    rules.add(rule);
    session.merge(newActivityEntity);
    tx1.commit();
    session.close();
    sessionFactory.close();