views:

323

answers:

2

Hello all,

I'm having trouble trying to persist an entity which ID is a generated value. This entity (A), at persistence time, has to persist in cascade another entity(B). The relationship within A and B is OneToMany, and the property related in B is part of a composite key.

I'm using Eclipse, JBOSS Runtime, JPA/Hibernate

Here is my code:

Entity A:

@Entity
public class Cambios implements Serializable {
    private static final long serialVersionUID = 1L;

    @SequenceGenerator(name="CAMBIOS_GEN",sequenceName="CAMBIOS_SEQ",allocationSize=1)
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CAMBIOS_GEN")
    @Column(name="ID_CAMBIO")
    private Long idCambio;

    //bi-directional many-to-one association to ObjetosCambio
    @OneToMany(cascade={CascadeType.PERSIST},mappedBy="cambios")
    private List<ObjetosCambio> objetosCambioList;  

    public Cambios() {
    }

    ...
} 

Entity B:

@Entity
@Table(name="OBJETOS_CAMBIO")
public class ObjetosCambio implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ObjetosCambioPK id;

    //bi-directional many-to-one association to Cambios
    @ManyToOne
    @JoinColumn(name="ID_CAMBIO", insertable=false, updatable=false)
    private Cambios cambios;

    //bi-directional many-to-one association to Objetos
    @ManyToOne
    @JoinColumn(name="ID_OBJETO", insertable=false, updatable=false)
    private Objetos objetos;

    public ObjetosCambio() {
    }

...

Entity B PK:

@Embeddable
public class ObjetosCambioPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="ID_OBJETO")
    private Long idObjeto;

    @Column(name="ID_CAMBIO")
    private Long idCambio;

    public ObjetosCambioPK() {
    }

Client:

public String generarCambio(){

        ServiceLocator serviceLocator = null;
        try {        
            serviceLocator = serviceLocator.getInstance();
            FachadaLocal tcLocal;
            tcLocal = (FachadaLocal)serviceLocator.getFacadeService("java:comp/env/negocio/Fachada"); 

            Cambios cambio = new Cambios();

            Iterator it = objetosLocal.iterator(); //OBJETOSLOCAL IS ALREADY POPULATED OUTSIDE OF THIS METHOD            
            List<ObjetosCambio> ocList = new ArrayList();            
            while (it.hasNext()){
                     Objetos objeto = (Objetos)it.next();
                     ObjetosCambio objetosCambio = new ObjetosCambio();
                     objetosCambio.setCambios(cambio); //AT THIS TIME THIS "CAMBIO" DOES NOT HAVE ITS ID, ITS SUPPOSED TO BE GENERATED AT PERSISTENCE TIME

                     ObjetosCambioPK ocPK = new ObjetosCambioPK();
                     ocPK.setIdObjeto(objeto.getIdObjeto());

                     objetosCambio.setId(ocPK); 
                     ocList.add(objetosCambio);
                                }  

            cambio.setObjetosCambioList(ocList); 

            tcLocal.persistEntity(cambio); 
            return "exito";            
                } catch (NamingException e) {
                    // TODO                
                    e.printStackTrace();                    
                                            }
         return null;
    }


ERROR:

15:23:25,717 WARN  [JDBCExceptionReporter] SQL Error: 1400, SQLState: 23000
15:23:25,717 ERROR [JDBCExceptionReporter] ORA-01400: no se puede realizar una inserción NULL en ("CDC"."OBJETOS_CAMBIO"."ID_CAMBIO")

15:23:25,717 WARN  [JDBCExceptionReporter] SQL Error: 1400, SQLState: 23000
15:23:25,717 ERROR [JDBCExceptionReporter] ORA-01400: no se puede realizar una inserción NULL en ("CDC"."OBJETOS_CAMBIO"."ID_CAMBIO")

15:23:25,717 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
    at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:269)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:89)
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1423)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:137)
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
    at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
    at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
    at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:176)
    at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:216)
    at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
    at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
    at $Proxy298.persistEntity(Unknown Source)
    at backing.SolicitudCambio.generarCambio(SolicitudCambio.java:521)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:146)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:92)
    at javax.faces.component.UICommand.broadcast(UICommand.java:332)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:287)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:401)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:95)
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:245)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:110)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:301)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.BatchUpdateException: ORA-01400: no se puede realizar una inserción NULL en ("CDC"."OBJETOS_CAMBIO"."ID_CAMBIO")

Thanks in advance! JM.-

+1  A: 

I don't think that the above code can work, the idCambio of your embeddable ObjetosCambioPK is simply never assigned, hence the database error. Actually I don't know how to achieve what you want in a "fully automated" way (I don't think it's doable) and I would thus persist() and flush() the new Cambios first and then set the assigned Id on the composite key of each ObjetosCambio instance.

Pascal Thivent
Thanks for your response, I once achieved this but it was with a different architecture (EJB3,Jdeveloper,Toplink), so I know this could work. Back then I used `@IdClass` annotation instead of `@EmbeddedId`, but I've read it's not the best approach nowadays, it seems that this @IdClass was only implemented in Hibernate because of EJB2 compatibility. Besides that, I'm having trouble with `@idClass` also... so I prefer to continue trying to find a solution for this one. Regarding your advice:
Steppen
"I would thus persist() and flush() the new Cambios first and then set the assigned Id on the composite key of each ObjetosCambio instance."If I do this the way you say, wouldn't I be risking leaving the DB in an inconsistent state? (given the case an exception ocurrs at `ObjetosCambio` persist time)
Steppen
@Steppen In case an exception ocurrs at `ObjetosCambio` persist time, the transaction will be rolled back and you won't be in an inconsistent state.
Pascal Thivent
Yes, `ObjetosCambio` tx will have a rollback, but what about `Cambio`? This one will be already persisted... so I will have Entity Cambio persisted without it's corresponding ObjetosCambio. Perhaps I can add extra code to explicitly say that both persist are a whole and cannot be done apart, but that's not what I'm looking for.
Steppen
@Steppen: I'm not following you. I never suggested to run two separate transactions, I suggested to `persist` and `flush` the `Cambio` to get the id assigned (flush != commit) before to persist `ObjetosCambio`. So in case an exception occurs, everything would be rolled back.
Pascal Thivent
I'm sorry, I missed that one. I really want to use only one persist to do this. But I will keep it in mind in case I do not have a solution in a couple of days. Thanks Pascal
Steppen
@Steppen: No problem. If you find a solution, I'd be really interested so please post it :)
Pascal Thivent
A: 

I tried to set cambios.idCambio manually but I'm still getting the same error. Hibernate should do some kind of mapping between objetosCambioPK.idCambio and objetosCambio.cambios. Perhaps that's wthat I'm missing here, and I don't know how to implement it.

Anyone?

Steppen