简体   繁体   English

TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例

[英]TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge

Can anybody explain me why I am getting TransientObjectException when I doing merge. 谁能解释我为什么在合并时会出现TransientObjectException。 Issue is reproducing only when I create RuleTestEntitiy inside constructor of ActivityTestEntity as showed below. 仅当我在ActivityTestEntity的构造函数中创建RuleTestEntitiy时,问题才重现,如下所示。 It doesn't appears if I do update or create. 如果我进行更新或创建,它不会出现。

Thanks in advance. 提前致谢。

Here is a test: 这是一个测试:

 @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(); } }

Exception: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xxx.RuleTestEntity 异常:org.hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例: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; } }

 @Entity @Table(name = "RULE_TEST") public class RuleTestEntity implements Serializable { private static final long serialVersionUID = -4208222848601642508L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "RULE_ID", nullable = false) @XmlElement(name = Identifiable.ID_FIELD_NAME) private long id; @ManyToOne @JoinColumn(name = "ACTIVITY_ID", nullable = true, updatable = false) @XmlTransient private ActivityTestEntity activity; public ActivityTestEntity getActivity() { return activity; } public void setActivity(ActivityTestEntity activity) { this.activity = activity; } public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RuleTestEntity that = (RuleTestEntity) o; if (id != that.id) { return false; } return activity != null ? activity.equals(that.activity) : that.activity == null; } @Override public int hashCode() { int result = (int) (id ^ (id >>> 32)); result = 31 * result + (activity != null ? activity.hashCode() : 0); return result; } } 

@Entity @Table(name = "RULE_TEST") public class RuleTestEntity implements Serializable { private static final long serialVersionUID = -4208222848601642508L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "RULE_ID", nullable = false) @XmlElement(name = Identifiable.ID_FIELD_NAME) private long id; @ManyToOne @JoinColumn(name = "ACTIVITY_ID", nullable = true, updatable = false) @XmlTransient private ActivityTestEntity activity; public ActivityTestEntity getActivity() { return activity; } public void setActivity(ActivityTestEntity activity) { this.activity = activity; } public long getId() { return id; } public void setId(long id) { this.id = id; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RuleTestEntity that = (RuleTestEntity) o; if (id != that.id) { return false; } return activity != null ? activity.equals(that.activity) : that.activity == null; } @Override public int hashCode() { int result = (int) (id ^ (id >>> 32)); result = 31 * result + (activity != null ? activity.hashCode() : 0); return result; } }

this exception is generated when you try to save an Entity referenced to an unsaved entity. 当您尝试保存引用未保存实体的实体时,将生成此异常。 you can either save the referenced entity first then save the entity or you can add cascade=CascadeType.All to the relations between the two of them. 您可以先保存引用的实体,然后再保存实体,或者可以在它们之间的关系中添加cascade=CascadeType.All

Remove the code inside your constructor and then try this code: 删除构造函数中的代码,然后尝试以下代码:

    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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing org.hibernate.TransientObjectException:object 引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例: - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? 如何在大型项目上修复“对象引用未保存的瞬态实例-在刷新之前保存瞬态实例”? - How can I fix “object references an unsaved transient instance - save the transient instance before flushing” on a large project? 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.entity.Role - object references an unsaved transient instance - save the transient instance before flushing: com.entity.Role 对象引用了一个未保存的临时实例-使用休眠空间在刷新之前保存该临时实例 - object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial 数据未保存:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - Data was not saved: object references an unsaved transient instance - save the transient instance before flushing Object 引用了未保存的瞬态实例 在刷新错误之前保存瞬态实例 - Object references an unsaved transient instance save the transient instance before flushing error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM