【问题标题】:[PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: models[PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给坚持:模型
【发布时间】:2014-02-20 14:55:43
【问题描述】:

请帮助设置模型类,因为我收到错误消息:

[PersistenceException: org.hibernate.PersistentObjectException: 分离的实体传递给持久化:models.IncidentType_Diagnosis]

当我尝试持久化模型类时出现错误:MedicalIncident.java 其中包含模型类 IncidentType_Diagnosis

package models;

@Entity
public class MedicalIncident {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;

@ManyToOne(cascade = CascadeType.MERGE)
@Required
public Customer customer;

@ManyToOne(cascade = CascadeType.MERGE)
@Required
public Place place;

@ManyToOne(cascade = CascadeType.MERGE)
@Required
public IncidentType incidentType;

@ManyToOne(cascade = CascadeType.ALL)
public IncidentType_Diagnosis incidentType_Diagnosis;

@ManyToOne(cascade = CascadeType.ALL)
public IncidentType_Infection incidentType_Infection;

@ManyToOne(cascade = CascadeType.ALL)
@Required
public IncidentType_MedicineApplication incidentType_MedicineApplication;



/**
 * Insert this new incident submission.
 */
public void toDataBase() {
    // persist object - add to entity manager
    JPA.em().persist(this);
}
}

以及 IncidentType_Diagnosis 的下一个模型类:

package models;
@Entity 

public class IncidentType_Diagnosis {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
    public String name;

}

我的控制器:

    public class MedicalIncidents extends Controller {
       /**
       * Handle the form submission.
       */
       @Transactional
       public static Result submit() {
            if(filled_form.hasErrors()) {
        return badRequest(form.render(filled_form, display));
            } else {
                    filled_form.get().toDataBase(); // calling in model method
                    return redirect(routes.Index.index());
            }
       }
    }

也是我与医疗事故模型类相关的模型类之一

package models;
@Entity 
public class IncidentType_Diagnosis {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long id;
    public String name;

    public static List<IncidentType_Diagnosis> getList()   {
        List<IncidentType_Diagnosis> allIncidentType_Diagnosiss = (List<IncidentType_Diagnosis>) JPA.em()
                .createNativeQuery("SELECT * FROM IncidentType_Diagnosis", IncidentType_Diagnosis.class)
                .getResultList();
        return allIncidentType_Diagnosiss;
    }
}

【问题讨论】:

  • 显示你实际保存对象的代码
  • package models -&gt; @Entity public class MedicalIncident -&gt; public void toDataBase()

标签: hibernate jpa playframework-2.2


【解决方案1】:

通常detached 模型意味着它不在您想要持久化对象的transaction scope 中。换句话说,你的transaction 挂在空中。因此,您需要使用@Transactional 使其具有事务性

已编辑:

不,模型应该只用于表格。 DAO方法应该在事务中,如果你写这样的东西,请在事务范围内为它写测试。

【讨论】:

  • @Transactional 注释设置在controlles -&gt; MedicalIncidents -&gt; submit() 我已经上传了上面的代码。是否也应该在模型中设置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2011-09-16
  • 2015-04-22
  • 2021-07-18
  • 2021-06-30
相关资源
最近更新 更多