Hibernate异常:传递给persist的分离实体 [英] Hibernate Exception : detached entity passed to persist

查看:612
本文介绍了Hibernate异常:传递给persist的分离实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  entityManagerTransactionService.getEntityManager()使用Hibernate EntityManager持久化方法插入一个人pojo到mysql数据库。 .persist(TemplateObject); 

得到这个异常,

<$ p $ javax.persistence.PersistenceException:
org.hibernate.PersistentObjectException:传递给
的分离实体persist:com.esupu.base.model.pojo.common.TitleType at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)....

还有更多..

 引起:org.hibernate.PersistentObjectException:分离的实体
传递给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.PE RSIST,CascadeType.REFRESH})
private TitleType titleType;
私有字符串中间;
私人字符串最后;
@OneToOne(cascade = {CascadeType.ALL})
私有地址;
私人日期birthdate;
私人字符串电子邮件;
私人弦乐电话;
私人字符串传真;
@OneToOne(cascade = {CascadeType.ALL})
私人位置位置;
@OneToOne(cascade = {CascadeType.ALL})
私人国家国家;

然后是pojo的getter setter方法..



错误引发TitleType类是一个公共静态final标题类型定义提供程序,

  @Entity 
public class TitleType extends TemplateObject {
$ b $ / **
*
* /
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);


我将标题类型类设置为人as,

  person.setTitleType(TitleType.MR); 

是不可能将公共静态最终类型定义的类传递给Hibernate?或者是否有什么我做错了?



提前致谢 >

您尝试持久化的对象包括 TitleType ,它们不应具有 ID 值。否则,Hibernate将检查该对象是否在会话上下文中持久或分离。如果你不想像这样坚持你的对象的某些字段,你可以使用 @Transient 注释来在持久化实体时从字段映射中排除这些字段。这对硬编码DAO或实体中的ID是不好的,因为这些值可能由某个生成器生成,但是如果使用此常量手动更新数据库,则至少可以在代码中将这些ID定义为常量,并根据ID从会话中加载对象在初始化瞬态或持久实例之前。

I'm trying to insert a person pojo to mysql db using Hibernate EntityManager persist method,

entityManagerTransactionService.getEntityManager().persist(TemplateObject);

and getting this exception,

 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)....

and there is more..

 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)

My Person.java class is,

@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;

and then the getter setter methods of the pojo ..

the error throwing TitleType class is a "public static final" title type definition provider,

@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");

}

I'm setting the title type class to the the person as,

person.setTitleType(TitleType.MR);

is it impossible to pass a public static final type defined class to Hibernate? or is there anything that I'm doing wrong?

Thanks in advance

解决方案

The objects you are trying to persist include TitleType that should not have ID values. Otherwise Hibernate will check to see if that object is persistent or detached in the session context. If you don't want to persist some fields of your object like that you could use @Transient annotation to exclude those fields from the fields map when persisting the entity. This is not good to hardcode IDs in the DAOs or entities because these values could be generated by some generator but if you manually updated the database with this constants you could at least define these IDs as constants in your code and load objects from session by ID before initialize the transient or persistent instance.

这篇关于Hibernate异常:传递给persist的分离实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆