日期:2014-05-20  浏览次数:20877 次

EJB实体Bean持久化的时候报错
后台报错信息:

15:22:53,750 WARN [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, IntrceptorRegistry should be used via the bean container
15:22:53,750 WARN [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, IntrceptorRegistry should be used via the bean container
15:22:53,750 WARN [InterceptorRegistry] applicable interceptors is non-existent for public huizhi.entity.Person huizhisessionbean.Personbean.getPersonByCid(java.lang.String)
15:22:53,765 WARN [InterceptorRegistry] applicable interceptors is non-existent for public void huizhi.sessionbean.Peronbean.updatePerson(huizhi.entity.Person,java.lang.String)
15:22:53,765 WARN [InterceptorRegistry] applicable interceptors is non-existent for public void huizhi.sessionbean.Peronbean.addPerson(java.lang.String,int,java.lang.String,java.lang.String,java.lang.String,java.util.Date)
15:22:53,765 WARN [InterceptorRegistry] applicable interceptors is non-existent for public huizhi.entity.Person huizhisessionbean.Personbean.getPersonByCid(java.lang.String)
15:22:53,781 WARN [InterceptorRegistry] applicable interceptors is non-existent for public void huizhi.sessionbean.Peronbean.updatePerson(huizhi.entity.Person,java.lang.String)
15:22:53,781 WARN [InterceptorRegistry] applicable interceptors is non-existent for public void huizhi.sessionbean.Peronbean.addPerson(java.lang.String,int,java.lang.String,java.lang.String,java.lang.String,java.util.Date)
15:22:53,781 INFO [STDOUT] -- 持久化过程中发生异常 --
15:22:53,781 INFO [STDOUT] org.hibernate.PersistentObjectException: detached entity passed to persist: huizhi.entity.Card
16:15:17,281 INFO [STDOUT] -- 持久化过程中发生异常 --
16:15:17,296 INFO [STDOUT] org.hibernate.PersistentObjectException: detached entity passed to persist: huizhi.entity.Card



出错所在的方法:
Java code

@PersistenceContext(unitName="YingShe_PU")protected EntityManager em;    
public void addPerson(String name, int age, String phone, String address,
            String cid, Date valDate) {
        Person p = new Person();
        Card c = new Card();
        c.setCid(cid);
        c.setValDate(valDate);
        c.setPerson(p);
        p.setName(name);
        p.setAddress(address);
        p.setAge(age);
        p.setCard(c);
        p.setPhone(phone);
        em.persist(p);
    }






------解决方案--------------------
像是级联保存的问题。
Card c = new Card();
c.setCid(cid);

先保存c.试试;
em.persist(c);
em.persist(p);

------解决方案--------------------
Card一端没有外键,外键在Person一端。
所以绝不能persist(Person).
只能persist(Card),继而级联添加Person.

但Card中的注解并没有设置级联添加:
@OneToOne(cascade={CascadeType.MERGE, CascadeType.REMOVE}, optional = false, mappedBy = "card")
private Person person;

应改为:
@OneToOne(cascade=CascadeType.ALL, optional = false, mappedBy = "card")
private Person person;

含有外键的一端应该加入unique表示一对一:
@JoinColumn(name = "cardid", referencedColumnName = "cid",unique="true")
private Card card;


另外,不知道你的数据库使用的是哪种?
如果是SQLServer,主键的注解应该是:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

如果是Oracle,主键的注解应该是:
@Id
@SequenceGenerator(name="oid",sequenceName="oid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="oid")

如果是MySQL则可使用AUTO.

添加时则不用指定card的主键值,只需要设置其他属性值即可:

//设置card属性值
//设置person属性值
card.setPerson(person);
person.setCard(card);
em.persist(card);