我試圖插入一個人POJO到MySQL數據庫使用Hibernate EntityManager的persist方法分離的實體,休眠例外:通過堅持
entityManagerTransactionService.getEntityManager().persist(TemplateObject);
,並得到這個例外,
javax.persistence.PersistenceException:
org.hibernate.PersistentObjectException: detached entity passed to
persist: com.esupu.base.model.pojo.common.TitleType at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)....
有更..
Caused by: org.hibernate.PersistentObjectException: detached entity
passed to persist: com.esupu.base.model.pojo.common.TitleType at
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
at
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843)
我Person.java類,
@Entity
public class Person extends TemplateObject {
@OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH })
private TitleType titleType;
private String middle;
private String last;
@OneToOne(cascade = { CascadeType.ALL })
private Address address;
private Date birthdate;
private String email;
private String telephone;
private String fax;
@OneToOne(cascade = { CascadeType.ALL })
private Location location;
@OneToOne(cascade = { CascadeType.ALL })
private Country country;
,然後POJO的吸氣setter方法..
扔TitleType類錯誤是「公共靜態最後的」標題類型定義提供商,
@Entity
public class TitleType extends TemplateObject {
/**
*
*/
private static final long serialVersionUID = 3104954552111621034L;
private TitleType(int id, String name) {
setId(id);
setName(name);
}
public static final TitleType MR = new TitleType(1, "Mr");
public static final TitleType MISS = new TitleType(2, "Miss");
public static final TitleType MRS = new TitleType(3, "Mrs");
public static final TitleType MS = new TitleType(4, "Ms");
public static final TitleType DOCTOR = new TitleType(5, "Doctor");
public static final TitleType PROFESSOR = new TitleType(6, "Professor");
public static final TitleType SIR = new TitleType(7, "Sir");
public static final TitleType MADAM = new TitleType(8, "Madam");
}
我設置標題類型類的人,
person.setTitleType(TitleType.MR);
是不可能將公共靜態最終類型定義類傳遞給Hibernate?或者有什麼我做錯了?
在此先感謝
我們展示的代碼來持久化對象。看起來你試圖堅持一個分離的對象 – Shamse