Announcement Announcement Module
Collapse
No announcement yet.
Help: getHibernateTemplate().save Problem! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help: getHibernateTemplate().save Problem!

    I have a problem with using getHibernateTemplate().save. in my code I use something like this:

    public class CustomisationDaoComponentImpl
    extends HibernateDaoSupport
    implements CustomisationService {

    public void save(final Customisation page) {
    getHibernateTemplate().save(page);
    addToLog("Object has been successfully saved into the database");
    }
    }

    and the bean configurations are something like this:
    <bean id="hibernateConfigProperties" class="org.springframework.beans.factory.config.Pr opertiesFactoryBean">
    <property name="locations">
    <list>
    <value>classpath:alfresco/domain/hibernate-cfg.properties</value>
    </list>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" parent="sessionFactoryBase">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    </bean>

    <bean id="sessionFactoryBase" abstract="true">
    <property name="schemaUpdate">
    <value>false</value>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/stratumcomms/guardian/repo/admin/hibernate/Customisation.hbm.xml</value>
    <property name="hibernateProperties" ref="hibernateConfigProperties" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="transactionSynchronizationName">
    <value>SYNCHRONIZATION_ALWAYS</value>
    </property>
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    <bean id="customisationDaoComponent" class="com.stratumcomms.guardian.repo.admin.hibern ate.CustomisationDaoComponentImpl" init-method="init">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    <bean id="customisationService" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
    <value>com.stratumcomms.guardian.service.admin.Cus tomisationService</value>
    </property>
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>
    <property name="target">
    <ref bean="customisationDaoComponent" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    <property name="proxyTargetClass" value="true"/>
    </bean>


    when I call the save() method as mentioned earlier, the log didn't show any error instead it displayed a successful message. However, when I queried the database directly, the new record wasn't there . Does anyone know how to fix this problem? I am totally confused because there was not any error/exception when getHibernateTemplate().save method was executed, but the execution result didn't reflect in the database or in short the object wasn't persisted into the database.

  • #2
    Same here -&gt; solved for me

    Hi,

    have exactly the same problem with MySQL 5.1. After hours searching, reading and trying my guess was to change the table engine from InnoDB to MyISAM. And IT WORKS now! Why ever, can't understand. Enybody can? So please tell me.

    What are the SpringSource guys use at there projects? What are your experiences?

    Next Problem occured by using the "getHibernateTemplate().save" for updating an existing value (at id=7, primary key). I try not to change the primary key, just another column at the db.

    Code:
    // 1. Create a new data set
    WebAppFrameworks webAppFrameworks = new WebAppFrameworks();
    int id = 7;
    webAppFrameworks.setId(id);
    webAppFrameworks.setName("myProject"+id);
    webAppFrameworks.setLanguage("my PHP"+id);
    			
    // 2. Load the Spring bean configuration and create a factory
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    System.out.println("... Classpath loaded");
    			
    // 3. Create instance of WebAppFrameworksDAO
    WebAppFrameworksDAO webAppFrameworksDAO = (WebAppFrameworksDAO) beanFactory.getBean("WebAppFrameworksDAO");
    
    // 4. Save the new data set to the database
    webAppFrameworksDAO.save(webAppFrameworks);
    			
    // 5. Confirm that our webAppFrameworks was saved
    WebAppFrameworks wafFromDB = webAppFrameworksDAO.findById(id);
    			
    // 6. Update the webAppFrameworks
    wafFromDB.setLanguage("my UPDATED language 2");
    webAppFrameworksDAO.save(wafFromDB);
    The webAppFrameworksDAO.save(xxx):
    Code:
    	public void save(WebAppFrameworks transientInstance) {
    		log.debug("saving WebAppFrameworks instance");
    		try {
    			getHibernateTemplate().save(transientInstance);
    			log.debug("save successful");
    		} catch (RuntimeException re) {
    			log.error("save failed", re);
    			throw re;
    		}
    	}


    Whats wrong?


    The error at 6. is:
    Code:
    org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:624)
    	at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:690)
    	at org.icefaces.jpa.WebAppFrameworksDAO.save(WebAppFrameworksDAO.java:35)
    	at internal.notimplementedyet.MainFrame.main(MainFrame.java:48)
    Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    	at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
    	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
    	... 4 more
    Caused by: java.sql.BatchUpdateException: Duplicate entry '7' for key 'PRIMARY'
    	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
    	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
    	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    	... 11 more
    CA
    Last edited by millstone; Jan 14th, 2009, 01:59 PM. Reason: Could not execute JDBC batch update

    Comment


    • #3
      i too having the same problem,

      getHibernateTemplate().save(transientInstance); does not given any error or exception, but the data doest not stored.
      how to verify, what it returns and how its works, any idea?

      Comment


      • #4
        update existing value

        Hi,

        how I mentioned earlier, I have problems with updating existing values.
        For my understanding, i don't have to load an object from db, modify it and write (saveOrUpdate) back to db. All i have to do is to create a new object at my code and write (saveOrUpdate) it to db and spring/hibernate will search for existing (updateable) or not existing value???

        Thx for help.

        Comment


        • #5
          Try using persist() method instead of save()

          Hi,
          When you create an object and set the primary key value which is already exists in the database, and then call save() method passing that object results in the DataIntegrityViolationException.

          Instead you can use persist() method which checks the primary key , if it exists already it will update the remaining COLUMN values, otherwise it will insert new row.
          Last edited by sivaprasad8080; Feb 3rd, 2009, 11:46 AM.

          Comment


          • #6
            Hi,

            get the same error for save and persist:

            Code:
             org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
            By the way, whats ment by "remaining values"... Insert if "NULL" or "" or " " at db?

            Comment


            • #7
              What is this.? ??

              what is different getSession & getHibernateTemplate..???

              Comment

              Working...
              X