请帮助设置模型类,因为我收到错误:
[PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: 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;
}
}
答案 0 :(得分:0)
通常detached
模型意味着它不在transaction scope
您想要保留对象的位置。换句话说,你的transaction
被悬挂在空中。因此,您需要使用@Transactional
编辑:
不,模型应仅适用于表格。 DAO方法应该在事务中,如果你写这样的东西,请在事务范围内为它编写测试。