[PersistenceException:org.hibernate.PersistentObjectException:传递给persist:models的分离实体
请帮助设置模型类,因为我遇到错误: [PersistenceException:org.hibernate.PersistentObjectException:传递给persist:models.IncidentType_诊断的分离实体] 尝试持久化模型类时出现错误:MedicalIncident.java 其中包含模型类IncidentType_Diagnosis[PersistenceException:org.hibernate.PersistentObjectException:传递给persist:models的分离实体,hibernate,jpa,playframework-2.2,Hibernate,Jpa,Playframework 2.2,请帮助设置模型类,因为我遇到错误: [PersistenceException:org.hibernate.PersistentObjectException:传递给persist:models.IncidentType_诊断的分离实体] 尝试持久化模型类时出现错误:MedicalIncident.java 其中包含模型类IncidentType_Diagnosis package models; @Entity public class MedicalIncident { @Id @Gen
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);
}
}
意外类型_诊断的下一个模型类:
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());
}
}
}
也是我的一个模型类,它与medicalincident模型类相关
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;
}
}
封装模型;
@实体
公共类意外事件类型诊断{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
公共长id;
公共字符串名称;
公共静态列表getList(){
List allIncidentType_Diagnosiss=(List)JPA.em()
.createNativeQuery(“从IncidentType\u Diagnosis中选择*”,IncidentType\u Diagnosis.class)
.getResultList();
返回allIncidentType_诊断;
}
}
通常分离的
模型意味着它不在您想要持久化对象的事务范围
中。换句话说,您的事务
挂在空中。因此您需要使用@transactional
使其具有事务性
编辑:
不,模型应该只用于表。DAO方法应该在事务中,而且如果您编写类似的内容,请在事务范围中为其编写测试。显示实际保存对象的代码。它位于
包模型->@Entity public class MedicalIncident->public void to database()中
@Transactional
adnotation是在controlles->MedicalIncidents->submit()中设置的。
我已经上传了上面的代码。是否也应该在模型中设置?