关系问题@OneToMany

时间:2016-10-19 18:01:16

标签: java hibernate jpa one-to-many

我有2个实体(SubModelo和Opcional)。 SubModelo可能有一个Opcionals列表,因此是单向关系。当我试图坚持SubModelo实体并且它在opcional列表上没有Opcional时,Hibernate会正常持续,但是如果我在列表中包含一些opcional,它就不会持久存在,并且会给出错误,指出opcional实体不太可能持久存在。 Hibernate为关系创建了第三个表,但不包含任何内容。

@Entity 
public class SubModelo implements Serializable { 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column 
    @Size(max = 30) 
    private String nome; 
    @NotNull 
    @Size (max = 9) 
    private String motor; 
    @NotNull 
    private boolean automatico;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Opcional> opcionais;
    22:49:56,633 ERROR [org.jboss.as.ejb3.invocation] (default task-10)         WFLYEJB0034: EJB Invocation failed on component SubModeloService for method     public br.com.ozelo.entidades.SubModelo br.com.ozelo.servico.SubModeloService.novoSubModelo(br.com.ozelo.entidades.SubModelo): javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.ozelo.entidades.Opcional
at     org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187)
at      org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:327)
at      org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
     Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.ozelo.entidades.Opcional
     at      org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
     at org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java:580)
    at br.com.ozelo.DAO.BasicDao.addEntity(BasicDao.java:22)
    at br.com.ozelo.DAO.SubModeloDao.novoSubModelo(SubModeloDao.java:24)
     at br.com.ozelo.servico.SubModeloService.novoSubModelo(SubModeloService.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
23:31:01,042 INFO [stdout] (default task-1) javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.ozelo.entidades.Opcional

0 个答案:

没有答案
相关问题