0 Replies Latest reply on Mar 25, 2007 6:00 PM by null null

    Unable to save data with Hibernate - JPA

    null null Newbie

      Hello,

      I am getting the following error when trying to update an object. An insert operation seems to work fine. The code for the bean was generated by the Hibernate eclipse plug in.

      Thanks for your help.

      javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [diamelle.common.service.Service.requestApprovers#201]
       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
       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 org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:176)
       at $Proxy20.persist(Unknown Source)
       at diamelle.common.service.ServiceDAOBean.persist(ServiceDAOBean.java:41)
       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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:299)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
       at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
       at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
       at $Proxy21.persist(Unknown Source)
       at diamelle.common.service.ServiceMgr.updateService(ServiceMgr.java:56)
       at diamelle.common.service.ServiceMgr$$FastClassByCGLIB$$28b3d4a2.invoke(<generated>)
       at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
       at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:693)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
       at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
       at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:628)
       at diamelle.common.service.ServiceMgr$$EnhancerByCGLIB$$53de77e5.updateService(<generated>)
       at test.diamelle.common.service.ServiceMgrTest.testUpdateService(ServiceMgrTest.java:50)
       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 junit.framework.TestCase.runTest(TestCase.java:164)
       at junit.framework.TestCase.runBare(TestCase.java:130)
       at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
       at junit.framework.TestResult$1.protect(TestResult.java:110)
       at junit.framework.TestResult.runProtected(TestResult.java:128)
       at junit.framework.TestResult.run(TestResult.java:113)
       at junit.framework.TestCase.run(TestCase.java:120)
       at junit.framework.TestSuite.runTest(TestSuite.java:228)
       at junit.framework.TestSuite.run(TestSuite.java:223)
       at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [diamelle.common.service.Service.requestApprovers#201]
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.loader.Loader.loadCollection(Loader.java:1992)
       at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
       at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
       at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
       at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
       at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
       at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
       at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
       at org.hibernate.type.CollectionType.getElementsIterator(CollectionType.java:201)
       at org.hibernate.type.CollectionType.getElementsIterator(CollectionType.java:193)
       at org.hibernate.engine.CascadingAction.getAllElementsIterator(CascadingAction.java:415)
       at org.hibernate.engine.CascadingAction.access$100(CascadingAction.java:27)
       at org.hibernate.engine.CascadingAction$8.getCascadableChildrenIterator(CascadingAction.java:299)
       at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:294)
       at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
       at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
       at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
       at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
       at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
       at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
       at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
       at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
       at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
       at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
       at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
       ... 48 more
      Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.request_approver requestapp0_ where requestapp0_.SERVICE_ID='201'' at line 1
       at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
       at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
       at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
       at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
       at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
       at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)
       at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
       at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
       at org.hibernate.loader.Loader.doQuery(Loader.java:662)
       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
       at org.hibernate.loader.Loader.loadCollection(Loader.java:1985)
       ... 76 more
      
      


      --- Code in DAO that calls the service Bean -----

      @Transactional( propagation = Propagation.REQUIRED, readOnly = false )
       public void persist(Service transientInstance) {
       log.debug("persisting Service instance");
       try {
       entityManager.persist(transientInstance);
       log.debug("persist successful");
       } catch (RuntimeException re) {
       re.printStackTrace();
       log.error("persist failed", re);
       throw re;
       }
       }
      


      ---- Service Bean ------

      @NamedQueries({
       @NamedQuery(
       name = "findChildServices",
       query = "select s from Service s where s.parentServiceId = :id"
       ),
       @NamedQuery(
       name = "findAll",
       query = "select s from Service s )"
       )
      })
      
      
      
      /**
       * Service class to represent services connecte to the IdM system.
       *
       */
      @Entity
      @Table(name = "service", uniqueConstraints = {})
      public class Service implements java.io.Serializable {
      
       // Fields
      
       private String serviceId;
      
       private String serviceName;
      
       private String status;
      
       private String locationIpAddress;
      
       private String companyOwnerId;
      
       private Date startDate;
      
       private Date endDate;
      
       private String licenseKey;
      
       private String serviceType;
      
       private String parentServiceId;
      
       private String rootResourceId;
      
       private String accessControlModel;
      
       private String param1;
      
       private String param2;
      
       private String param3;
      
       private String param4;
      
       private String param5;
      
       private Set<RequestApprover> requestApprovers = new HashSet<RequestApprover>(0);
      
       private Set<ServiceConfig> serviceConfigs = new HashSet<ServiceConfig>(0);
      
       private Set<RequestForm> requestForms = new HashSet<RequestForm>(0);
      
       // Constructors
      
       /** default constructor */
       public Service() {
       }
      
       /** minimal constructor */
       public Service(String serviceId) {
       this.serviceId = serviceId;
       }
      
       /** full constructor */
       public Service(String serviceId, String serviceName, String status,
       String locationIpAddress, String companyOwnerId, Date startDate,
       Date endDate, String licenseKey, String serviceType,
       String parentServiceId, String rootResourceId,
       String accessControlModel, String param1, String param2,
       String param3, String param4, String param5,
       Set<RequestApprover> requestApprovers,
       Set<ServiceConfig> serviceConfigs, Set<RequestForm> requestForms) {
       this.serviceId = serviceId;
       this.serviceName = serviceName;
       this.status = status;
       this.locationIpAddress = locationIpAddress;
       this.companyOwnerId = companyOwnerId;
       this.startDate = startDate;
       this.endDate = endDate;
       this.licenseKey = licenseKey;
       this.serviceType = serviceType;
       this.parentServiceId = parentServiceId;
       this.rootResourceId = rootResourceId;
       this.accessControlModel = accessControlModel;
       this.param1 = param1;
       this.param2 = param2;
       this.param3 = param3;
       this.param4 = param4;
       this.param5 = param5;
       this.requestApprovers = requestApprovers;
       this.serviceConfigs = serviceConfigs;
       this.requestForms = requestForms;
       }
      
       // Property accessors
       @Id
       @Column(name = "SERVICE_ID", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
       public String getServiceId() {
       return this.serviceId;
       }
      
       public void setServiceId(String serviceId) {
       this.serviceId = serviceId;
       }
      
       @Column(name = "SERVICE_NAME", unique = false, nullable = true, insertable = true, updatable = true, length = 40)
       public String getServiceName() {
       return this.serviceName;
       }
      
       public void setServiceName(String serviceName) {
       this.serviceName = serviceName;
       }
      
       @Column(name = "STATUS", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getStatus() {
       return this.status;
       }
      
       public void setStatus(String status) {
       this.status = status;
       }
      
       @Column(name = "LOCATION_IP_ADDRESS", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getLocationIpAddress() {
       return this.locationIpAddress;
       }
      
       public void setLocationIpAddress(String locationIpAddress) {
       this.locationIpAddress = locationIpAddress;
       }
      
       @Column(name = "COMPANY_OWNER_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getCompanyOwnerId() {
       return this.companyOwnerId;
       }
      
       public void setCompanyOwnerId(String companyOwnerId) {
       this.companyOwnerId = companyOwnerId;
       }
      
       @Column(name = "START_DATE", unique = false, nullable = true, insertable = true, updatable = true, length = 19)
       public Date getStartDate() {
       return this.startDate;
       }
      
       public void setStartDate(Date startDate) {
       this.startDate = startDate;
       }
      
       @Column(name = "END_DATE", unique = false, nullable = true, insertable = true, updatable = true, length = 19)
       public Date getEndDate() {
       return this.endDate;
       }
      
       public void setEndDate(Date endDate) {
       this.endDate = endDate;
       }
      
       @Column(name = "LICENSE_KEY", unique = false, nullable = true, insertable = true, updatable = true)
       public String getLicenseKey() {
       return this.licenseKey;
       }
      
       public void setLicenseKey(String licenseKey) {
       this.licenseKey = licenseKey;
       }
      
       @Column(name = "SERVICE_TYPE", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getServiceType() {
       return this.serviceType;
       }
      
       public void setServiceType(String serviceType) {
       this.serviceType = serviceType;
       }
      
       @Column(name = "PARENT_SERVICE_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getParentServiceId() {
       return this.parentServiceId;
       }
      
       public void setParentServiceId(String parentServiceId) {
       this.parentServiceId = parentServiceId;
       }
      
       @Column(name = "ROOT_RESOURCE_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getRootResourceId() {
       return this.rootResourceId;
       }
      
       public void setRootResourceId(String rootResourceId) {
       this.rootResourceId = rootResourceId;
       }
      
       @Column(name = "ACCESS_CONTROL_MODEL", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
       public String getAccessControlModel() {
       return this.accessControlModel;
       }
      
       public void setAccessControlModel(String accessControlModel) {
       this.accessControlModel = accessControlModel;
       }
      
       @Column(name = "PARAM1", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getParam1() {
       return this.param1;
       }
      
       public void setParam1(String param1) {
       this.param1 = param1;
       }
      
       @Column(name = "PARAM2", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getParam2() {
       return this.param2;
       }
      
       public void setParam2(String param2) {
       this.param2 = param2;
       }
      
       @Column(name = "PARAM3", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getParam3() {
       return this.param3;
       }
      
       public void setParam3(String param3) {
       this.param3 = param3;
       }
      
       @Column(name = "PARAM4", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getParam4() {
       return this.param4;
       }
      
       public void setParam4(String param4) {
       this.param4 = param4;
       }
      
       @Column(name = "PARAM5", unique = false, nullable = true, insertable = true, updatable = true, length = 80)
       public String getParam5() {
       return this.param5;
       }
      
       public void setParam5(String param5) {
       this.param5 = param5;
       }
      
      
      
       @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "service")
       public Set<RequestApprover> getRequestApprovers() {
       return this.requestApprovers;
       }
      
       public void setRequestApprovers(Set<RequestApprover> requestApprovers) {
       this.requestApprovers = requestApprovers;
       }
      
       @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "service")
       public Set<ServiceConfig> getServiceConfigs() {
       return this.serviceConfigs;
       }
      
       public void setServiceConfigs(Set<ServiceConfig> serviceConfigs) {
       this.serviceConfigs = serviceConfigs;
       }
      
       @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "service")
       public Set<RequestForm> getRequestForms() {
       return this.requestForms;
       }
      
       public void setRequestForms(Set<RequestForm> requestForms) {
       this.requestForms = requestForms;
       }
      
      }