8 Replies Latest reply on May 25, 2006 1:35 PM by yantriki

    Question about merge

      I want to modify the ModuleProperty collection of the Module class and then merge it back. When I use the

      public Module updateModule(Module module){
       em.merge(module) ;
       return module;
      }
      


      in my stateless session bean, it throws exception (attached at the bottom of the post). When I individually persist or merge each ModuleProperty in the collection, it works, however I am getting "failed batch" exception when I try to remove a ModuleProperty using em.remove(property) and also removing from module
      module.getProperties().remove(property)
      then call merge on the module. Am I doing something wrong?

      I have the following two classes:
      @Entity
      @Table(name="Module")
      public class Module implements Serializable{
      
       @Id
       @GeneratedValue
       int moduleID;
      
       @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
       @JoinColumn(name="MODULE_ID")
       Collection<ModuleProperty> properties = new HashSet<ModuleProperty>();
      
       public int getModuleID() {
       return moduleID;
       }
       public void setModuleID(int moduleID) {
       this.moduleID = moduleID;
       }
      
       public Collection<ModuleProperty> getProperties() {
       return properties;
       }
       public void setProperties(Collection<ModuleProperty> properties) {
       this.properties = properties;
       }
      }
      


      @Entity
      @Table(name="ModuleProperty")
      public class ModuleProperty implements Serializable {
       /**
       *
       */
       private static final long serialVersionUID = 1L;
       @EmbeddedId
       @AttributeOverrides({
       @AttributeOverride(name="moduleID", column=@Column(name="MODULE_ID")),
       @AttributeOverride(name="name", column=@Column(name="NAME"))
       })
       ModulePropertyPK id;
       String value;
       public ModulePropertyPK getId() {
       return id;
       }
       public void setId(ModulePropertyPK id) {
       this.id = id;
       }
       public String getValue() {
       return value;
       }
       public void setValue(String value) {
       this.value = value;
       }
      }
      


      @Embeddable
      public class ModulePropertyPK implements Serializable{
       int moduleID;
       String name;
       public String getName() {
       return name;
       }
       public void setName(String name) {
       this.name = name;
       }
      
       @ManyToOne
       @JoinColumn(name="MODULE_ID",nullable=false)
       public int getModuleID() {
       return moduleID;
       }
       public void setModuleID(int moduleID) {
       this.moduleID = moduleID;
       }
      
       @Override
       public int hashCode() {
       String tmp = name +moduleID;
       return tmp.hashCode();
       }
      
       @Override
       public boolean equals(Object obj) {
       if(obj instanceof ModulePropertyPK){
       ModulePropertyPK pk = (ModulePropertyPK)obj;
       return (pk.moduleID == this.moduleID && this.name != null && name.equals(pk.name));
       }
       return false;
       }
      }
      
      



      4:54:01,718 ERROR [JDBCExceptionReporter] failed batch
      14:54:01,718 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.GenericJDBCException: could not insert: [org.vss.ejb3.eb.ModuleProperty]
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2140)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2503)
      at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:473)
      at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1488)
      at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1107)
      at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:321)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:167)
      at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:100)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:225)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:55)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102)
      at $Proxy155.updateElement(Unknown Source)
      at org.vss.mbean.CORViewService.getHMSElementsAlarm(CORViewService.java:568)
      at org.vss.mbean.CORViewService.scanHMSElement(CORViewService.java:389)
      at org.vss.mbean.CORViewService.scanElement(CORViewService.java:283)
      at org.vss.mbean.CORViewService$PollingThread.run(CORViewService.java:231)
      Caused by: java.sql.BatchUpdateException: failed batch
      at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
      at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
      at org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeBatch(CachedPreparedStatement.java:478)
      at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:517)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
      at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:34)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2120)
      ... 39 more
      14:54:01,718 ERROR [STDERR] java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=OPT0051/3210, BranchQual=, localId=3210] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [org.vss.ejb3.eb.ModuleProperty])
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(TxPolicy.java:198)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:180)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:167)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:100)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:225)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:55)
      14:54:01,718 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      14:54:01,718 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102)
      14:54:01,718 ERROR [STDERR] at $Proxy155.updateElement(Unknown Source)
      14:54:01,718 ERROR [STDERR] at org.vss.mbean.CORViewService.getHMSElementsAlarm(CORViewService.java:568)
      14:54:01,718 ERROR [STDERR] at org.vss.mbean.CORViewService.scanHMSElement(CORViewService.java:389)
      14:54:01,718 ERROR [STDERR] at org.vss.mbean.CORViewService.scanElement(CORViewService.java:283)
      14:54:01,718 ERROR [STDERR] at org.vss.mbean.CORViewService$PollingThread.run(CORViewService.java:231)
      14:54:01,718 ERROR [STDERR] Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=OPT0051/3210, BranchQual=, localId=3210] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [org.vss.ejb3.eb.ModuleProperty])
      14:54:01,718 ERROR [STDERR] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:369)
      14:54:01,734 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
      14:54:01,734 ERROR [STDERR] ... 25 more
      14:54:01,734 ERROR [STDERR] Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [org.vss.ejb3.eb.ModuleProperty]
      14:54:01,734 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
      14:54:01,734 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:482)
      14:54:01,734 ERROR [STDERR] at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1488)
      14:54:01,734 ERROR [STDERR] at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1107)
      14:54:01,734 ERROR [STDERR] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:321)
      14:54:01,734 ERROR [STDERR] ... 26 more
      14:54:01,734 ERROR [STDERR] Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [org.vss.ejb3.eb.ModuleProperty]
      14:54:01,734 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
      14:54:01,734 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
      14:54:01,734 ERROR [STDERR] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      14:54:01,734 ERROR [STDERR] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2140)
      14:54:01,734 ERROR [STDERR] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2503)
      14:54:01,734 ERROR [STDERR] at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
      14:54:01,734 ERROR [STDERR] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
      14:54:01,734 ERROR [STDERR] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
      14:54:01,734 ERROR [STDERR] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
      14:54:01,734 ERROR [STDERR] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
      14:54:01,734 ERROR [STDERR] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      14:54:01,734 ERROR [STDERR] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
      14:54:01,734 ERROR [STDERR] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
      14:54:01,734 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:473)
      14:54:01,734 ERROR [STDERR] ... 29 more
      14:54:01,734 ERROR [STDERR] Caused by: java.sql.BatchUpdateException: failed batch
      14:54:01,734 ERROR [STDERR] at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
      14:54:01,734 ERROR [STDERR] at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
      14:54:01,734 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeBatch(CachedPreparedStatement.java:478)
      14:54:01,734 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:517)
      14:54:01,734 ERROR [STDERR] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
      14:54:01,734 ERROR [STDERR] at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:34)
      14:54:01,734 ERROR [STDERR] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2120)
      14:54:01,734 ERROR [STDERR] ... 39 more
      14:54:01,734 INFO [CORViewService] Waiting for element polling queue...


        • 1. Re: Question about merge

          My test fails on the "testModifyModules". Is there something that I am doing wrong?

          Following is my Stateless Bean:

          /*
           * Created on May 23, 2006
           *
           * This distribution may include materials developed by third parties.
           *
           * Copyright (c) 2004 Vivek Srivastav. All rights reserved.
           * U.S. - Commercial software. Use is subject to license terms.
           */
          package org.test.ejb3.sb;
          
          import java.util.Collection;
          
          import javax.ejb.Remote;
          import javax.ejb.Stateless;
          import javax.persistence.EntityManager;
          import javax.persistence.PersistenceContext;
          
          import org.test.ejb3.eb.Module;
          
          @Stateless
          @Remote(ModuleManager.class)
          public class ModuleManagerBean implements ModuleManager {
           @PersistenceContext(unitName = "test")
           protected EntityManager em;
          
           @SuppressWarnings("unchecked")
           public Collection<Module> getModules() {
           return em.createQuery("from Module m").getResultList();
           }
          
           public Module addModule(Module m) {
           em.persist(m);
           return m;
           }
          
           public Module updateModule(Module m) {
           em.merge(m);
           return m;
           }
          
           public void removeModule(Module m) {
           em.remove(m);
           }
          
           public Module getModule(int moduleID) {
           return em.find(Module.class,moduleID);
           }
          
          }
          


          and the junit test case:

          /*
           * Created on May 23, 2006
           *
           * This distribution may include materials developed by third parties.
           *
           * Copyright (c) 2004 Vivek Srivastav. All rights reserved.
           * U.S. - Commercial software. Use is subject to license terms.
           */
          package org.test.ejb3.sb;
          
          import java.util.Collection;
          import java.util.HashSet;
          import java.util.Properties;
          import java.util.logging.Logger;
          
          import javax.naming.Context;
          import javax.naming.InitialContext;
          
          import org.test.ejb3.eb.Module;
          import org.test.ejb3.eb.ModuleProperty;
          import org.test.ejb3.eb.ModulePropertyPK;
          
          import junit.framework.Assert;
          import junit.framework.TestCase;
          
          public class ModuleManagerTest extends TestCase {
          
           InitialContext context;
          
           /*
           * Test method for 'org.test.ejb3.sb.ModuleManagerBean.getModules()'
           */
           public void testGetModules() {
           try {
           ModuleManager manager = (ModuleManager) context
           .lookup("cortest/ModuleManagerBean/remote");
           Collection<Module> modules = manager.getModules();
           int size = modules.size();
           Assert.assertTrue("The modules count is not 10",size==modules.size());
           } catch (Exception ex) {
           ex.printStackTrace();
           Assert.fail("Test Failed: " + ex.getClass().getSimpleName()+":"+ex.getMessage());
           }
           }
          
           public void testAddRemoveModules() {
           try {
           ModuleManager manager = (ModuleManager) context
           .lookup("cortest/ModuleManagerBean/remote");
           Module m = manager.addModule(new Module());
           Assert.assertTrue("Failed to add module, returned null",m!=null);
           Assert.assertTrue("Module ID is not set",m.getModuleID()!=0);
           manager.removeModule(m);
           m = manager.getModule(m.getModuleID());
           Assert.assertTrue("Module not deleted.",m==null);
           } catch (Exception ex) {
           ex.printStackTrace();
           Assert.fail("Test Failed: " + ex.getClass().getSimpleName()+":"+ex.getMessage());
           }
           }
          
           public void testModifyModules(){
           try {
           ModuleManager manager = (ModuleManager) context
           .lookup("cortest/ModuleManagerBean/remote");
           Collection<Module> modules = manager.getModules();
           for(Module module: modules){
           addProperties(module);
           module = manager.updateModule(module);
           updateProperties(module);
           module = manager.updateModule(module);
           removeProperties(module);
           module = manager.updateModule(module);
           }
           } catch (Exception ex) {
           ex.printStackTrace();
           Assert.fail("Test Failed: " + ex.getClass().getSimpleName()+":"+ex.getMessage());
           }
           }
          
           private void updateProperties(Module module) {
           // TODO Auto-generated method stub
           Collection<ModuleProperty>properties = module.getProperties();
           int count = (int)Math.random()*properties.size();
           Logger.global.info("Updating "+count+" properties out off tatal:"+ properties.size()+" for module: "+module.getModuleID());
           for(ModuleProperty p: properties){
           p.setValue("UPDATED VAL: "+count--);
           if(count==0)
           break;
           }
           }
          
           private void removeProperties(Module module) {
           Collection<ModuleProperty>properties = module.getProperties();
           Collection<ModuleProperty>new_props = new HashSet<ModuleProperty>();
           for(ModuleProperty p : properties){
           if(p.getValue().startsWith("UPDATED"))
           new_props.add(p);
           }
           module.setProperties(new_props);
           }
          
           private void addProperties(Module module) {
           // TODO Auto-generated method stub
           Collection<ModuleProperty>properties = module.getProperties();
           if(properties == null){
           properties = new HashSet<ModuleProperty>();
           }
           int count = (int)(Math.random()*100);
           Logger.global.info("Adding "+count+" properties to module: "+module.getModuleID());
           for(int i=0;i<count;i++){
           ModuleProperty p = new ModuleProperty();
           p.setId(new ModulePropertyPK());
           p.getId().setName("PROP"+i);
           p.setValue("VAL"+i);
           properties.add(p);
           }
           module.setProperties(properties);
           }
          
           @Override
           protected void setUp() throws Exception {
           // TODO Auto-generated method stub
           super.setUp();
           Properties jndiProperties = new Properties();
           jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY,
           "org.jnp.interfaces.NamingContextFactory");
           jndiProperties.put(Context.URL_PKG_PREFIXES,
           "org.jboss.naming:org.jnp.interfaces");
           jndiProperties.put(Context.PROVIDER_URL, "localhost:1099");
           context = new InitialContext(jndiProperties);
           ModuleManager manager = (ModuleManager) context
           .lookup("cortest/ModuleManagerBean/remote");
           for(int i=0;i<10;i++)
           manager.addModule(new Module());
           }
          
           @Override
           protected void tearDown() throws Exception {
           // TODO Auto-generated method stub
           super.tearDown();
           }
          
          }
          


          • 2. Re: Question about merge

            I get the following trace:

            junit.framework.AssertionFailedError: Test Failed: RuntimeException:org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=OPT0051/244, BranchQual=, localId=244] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [org.test.ejb3.eb.ModuleProperty])

            • 3. Re: Question about merge

              Here is my persistence.xml file.

              <persistence>
               <persistence-unit name="test">
               <jta-data-source>java:/DefaultDS</jta-data-source>
               <class>org.test.ejb3.eb.Module</class>
               <class>org.test.ejb3.eb.ModulePropertyPK</class>
               <class>org.test.ejb3.eb.ModuleProperty</class>
               <properties>
               <property name="hibernate.dialect"
               value="org.hibernate.dialect.HSQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
               </properties>
               </persistence-unit>
              </persistence>
              
              


              Here is the message I get on console when I deploy the application. I wonder if it is because of the JDBC batch update size?

              13:39:36,062 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
              13:39:36,062 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
              13:39:36,062 INFO [SettingsFactory] JDBC batch size: 15
              13:39:36,062 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
              13:39:36,062 INFO [SettingsFactory] Scrollable result sets: enabled
              13:39:36,062 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
              13:39:36,062 INFO [SettingsFactory] Connection release mode: auto
              13:39:36,062 INFO [SettingsFactory] Default batch fetch size: 1
              


              • 4. Re: Question about merge
                tzablock

                @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
                @JoinColumn(name="MODULE_ID")
                Collection properties = new HashSet();

                Try placing the @JoinColumn annotation on the @ManyToOne side and used mapped attribute in @OneToMany

                • 5. Re: Question about merge
                  tzablock

                  Oh, and try to specify the cascade=CascadeType.ALL in the @OneToMany annotation

                  • 6. Re: Question about merge

                    How do I use a mappedBy for a PK which is composite and one of the fields in the composite key (that forms a FK relationship) needs to be mapped:

                    Module {
                    @Id int moduleID
                    
                    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=?)
                    @MapKey(name="name"
                    Map<String,ModuleProperty> properties = new HashMap<String,ModuleProperty>();
                    
                    ..
                    ..
                    }
                    
                    @Embeddable
                    ModulePropertyPK {
                     @ManyToOne
                     @JoinColumn(name="MODULEID")
                     Module module;
                     String name;
                    ..
                    ..
                    }
                    
                    ModuleProperty {
                    @EmbeddedId
                     @AttributeOverrides( {
                     @AttributeOverride(name = "module", column = @Column(name = "MODULEID")),
                     @AttributeOverride(name = "name", column = @Column(name = "NAME")) })
                    ModulePropertyPK id;
                    ..
                    ..
                    }
                    


                    any attempt of trying to put a getter/setter for module in ModuleProperty results in a column called "MODULE" in the MODULEPROPERTY table. If I don't have the module getter/setter in the ModuleProperty class, the deployer throws missing property exception. Isn't the @AttributeOverrides meta-tag supposed to provide that mapped property?



                    • 7. Re: Question about merge
                      tzablock

                      I had some problems with creating composite fk mapping so what i always do is adding an artificial id column in such entity and then the composite mapping is not needed.

                      • 8. Re: Question about merge

                        The following seems to work for me:

                        @Entity
                        @Table(name = "ModuleProperty")
                        public class ModuleProperty implements Serializable {
                        
                         /**
                         *
                         */
                         private static final long serialVersionUID = 1L;
                        
                         ModulePropertyPK id;
                        
                         Module module;
                        
                         String value;
                        
                         public ModuleProperty() {
                         super();
                         id = new ModulePropertyPK();
                         }
                        
                         public String getValue() {
                         return value;
                         }
                        
                         public void setValue(String value) {
                         this.value = value;
                         }
                        
                         @EmbeddedId
                         @AttributeOverrides({
                         @AttributeOverride(name="moduleID", column=@Column(name="MODULEID")),
                         @AttributeOverride(name="name", column=@Column(name="NAME"))
                         })
                         public ModulePropertyPK getId() {
                         return id;
                         }
                        
                         public void setId(ModulePropertyPK id) {
                         this.id = id;
                         }
                        
                         @ManyToOne()
                         @JoinColumn(name="MODULEID",updatable=false,insertable=false)
                         public Module getModule() {
                         return module;
                         }
                        
                         public void setModule(Module module) {
                         this.module = module;
                         id.setModuleID(module.getModuleID());
                         }
                        
                         @Column(name="NAME",updatable=false,insertable=false)
                         public String getName(){
                         return id.getName();
                         }
                        
                         public void setName(String name){
                         id.setName(name);
                         }
                        }
                        

                        using @Column(updatable=false,insertable=false)
                        helped with mapping the FK to the field in composite key. However on the SLSB side I have to do the following to update changes to module properties:
                         public Module updateModule(Module m) {
                         try {
                         Module module = em.find(Module.class,m.getModuleID());
                         Logger.getLogger(this.getClass()).info(
                         "Updating module: " + m.getModuleID());
                         Map<String, ModuleProperty> properties = m.getProperties();
                         Map<String, ModuleProperty> props = module.getProperties();
                         for (ModuleProperty p : properties.values()) {
                         if(!props.containsKey(p.getName())){
                         p.setModule(module);
                         em.persist(p);
                         props.put(p.getName(),p);
                         } else{
                         props.get(p.getName()).setValue(p.getValue());
                         em.merge(props.get(p.getName()));
                         }
                         }
                         Set<String> keysToRemove = new HashSet<String>();
                         for(String key: props.keySet()){
                         if(!properties.containsKey(key)){
                         keysToRemove.add(key);
                         }
                         }
                         for(String key: keysToRemove){
                         em.remove(props.get(key));
                         props.remove(key);
                         }
                         module.setProperties(props);
                         em.merge(module);
                         return module;
                         } catch (Exception ex) {
                         ex.printStackTrace();
                         }
                         return null;
                         }
                        


                        If I don't do this the update throws batch update exception.
                        I would assume that merge should have handle that.