Announcement Announcement Module
Collapse
No announcement yet.
Synchronization error with Hibernate and spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Synchronization error with Hibernate and spring

    hello all,
    i am trying to write a DAO that uses spring and hibernate to update a database...
    i am still at JUnit test phase, and i am receiving the following exception whenever i try to insert data into the database

    bean 'Sybase'
    (support.SQLErrorCodesFactory 167 ) SQLErrorCodes loaded: [HSQL Database
    Engine, Oracle, Sybase SQL Server, Microsoft SQL Server, Informix Dynamic Server
    , PostgreSQL, DB2*, MySQL]
    (support.SQLErrorCodesFactory 197 ) Looking up default SQLErrorCodes for
    DataSource
    (support.SQLErrorCodesFactory 263 ) Database Product Name is MySQL
    (support.SQLErrorCodesFactory 264 ) Driver Version is mysql-connector-jav
    a-3.0.11-stable ( $Date: 2004/02/04 02:47:36 $, $Revision: 1.27.2.34 $ )
    Callingt testing persistence manager...
    a today date isThu Oct 07 19:58:48 GMT 2004
    (hibernate.HibernatePersistenceManager 36 ) INSERTING ENTRY...:TestExpense
    (impl.SessionImpl 2379) Could not synchronize database state
    with session
    net.sf.hibernate.HibernateException: Batch update row count wrong: 0
    at net.sf.hibernate.impl.BatchingBatcher.doExecuteBat ch(BatchingBatcher.
    java:65)
    at net.sf.hibernate.impl.BatcherImpl.executeBatch(Bat cherImpl.java:126)
    at net.sf.hibernate.impl.SessionImpl.executeAll(Sessi onImpl.java:2421)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionI mpl.java:2372)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImp l.java:2240)
    at org.springframework.orm.hibernate.HibernateAccesso r.flushIfNecessary(
    HibernateAccessor.java:214)
    at org.springframework.orm.hibernate.HibernateTemplat e.execute(Hibernate
    Template.java:177)
    at org.springframework.orm.hibernate.HibernateTemplat e.saveOrUpdate(Hibe
    rnateTemplate.java:317)
    at com.myapp.hibernate.HibernatePersistenceManager.in sert(HibernatePersi
    stenceManager.java:107)
    at com.myapp.hibernate.BaseHibernateTest.testHello(Ba seHibernateTest.jav
    a:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.
    java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.run(JUni
    tTestRunner.java:325)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnit Task.executeInVM(JU
    nitTask.java:848)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnit Task.execute(JUnitT
    ask.java:556)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnit Task.execute(JUnitT
    ask.java:532)
    at org.apache.tools.ant.Task.perform(Task.java:341)
    at org.apache.commons.jelly.tags.ant.AntTag.doTag(Ant Tag.java:232)
    at org.apache.commons.jelly.impl.TagScript.run(TagScr ipt.java:279)
    at org.apache.commons.jelly.impl.ScriptBlock.run(Scri ptBlock.java:135)
    at org.apache.commons.jelly.TagSupport.invokeBody(Tag Support.java:233)
    at org.apache.commons.jelly.tags.core.IfTag.doTag(IfT ag.java:88)
    at org.apache.commons.jelly.impl.TagScript.run(TagScr ipt.java:279)
    at org.apache.commons.jelly.impl.ScriptBlock.run(Scri ptBlock.java:135)
    at org.apache.commons.jelly.TagSupport.invokeBody(Tag Support.java:233)
    at com.werken.werkz.jelly.GoalTag$1.performAction(Goa lTag.java:128)
    at com.werken.werkz.Goal.fire(Goal.java:639)
    at com.werken.werkz.Goal.attain(Goal.java:575)
    at com.werken.werkz.Goal.attainPrecursors(Goal.java:4 88)
    at com.werken.werkz.Goal.attain(Goal.java:573)
    at com.werken.werkz.Goal.attainPrecursors(Goal.java:4 88)
    at com.werken.werkz.Goal.attain(Goal.java:573)
    at com.werken.werkz.WerkzProject.attainGoal(WerkzProj ect.java:193)
    at com.werken.werkz.jelly.AttainGoalTag.doTag(AttainG oalTag.java:134)
    at org.apache.maven.jelly.tags.werkz.LazyAttainGoalTa g.doTag(LazyAttainG
    oalTag.java:107)
    at org.apache.commons.jelly.impl.TagScript.run(TagScr ipt.java:279)
    at org.apache.commons.jelly.impl.ScriptBlock.run(Scri ptBlock.java:135)
    at org.apache.commons.jelly.TagSupport.invokeBody(Tag Support.java:233)
    at com.werken.werkz.jelly.GoalTag$1.performAction(Goa lTag.java:128)
    at com.werken.werkz.Goal.fire(Goal.java:639)
    at com.werken.werkz.Goal.attain(Goal.java:575)
    at org.apache.maven.plugin.PluginManager.attainGoals( PluginManager.java:
    448)
    at org.apache.maven.MavenSession.attainGoals(MavenSes sion.java:360)
    at org.apache.maven.jelly.tags.maven.ReactorTag.doTag (ReactorTag.java:37
    8)
    at org.apache.commons.jelly.impl.TagScript.run(TagScr ipt.java:279)
    at org.apache.commons.jelly.impl.ScriptBlock.run(Scri ptBlock.java:135)
    at org.apache.commons.jelly.TagSupport.invokeBody(Tag Support.java:233)
    at com.werken.werkz.jelly.GoalTag$1.performAction(Goa lTag.java:128)
    at com.werken.werkz.Goal.fire(Goal.java:639)
    at com.werken.werkz.Goal.attain(Goal.java:575)
    at org.apache.maven.plugin.PluginManager.attainGoals( PluginManager.java:
    448)
    at org.apache.maven.MavenSession.attainGoals(MavenSes sion.java:348)
    at org.apache.maven.cli.App.doMain(App.java:543)
    at org.apache.maven.cli.App.main(App.java:1109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.
    java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at com.werken.forehead.Forehead.run(Forehead.java:551 )
    at com.werken.forehead.Forehead.main(Forehead.java:58 1)
    (hibernate.HibernatePersistenceManager 36 ) Exception in creating HibernateEntr
    y
    org.springframework.orm.hibernate.HibernateSystemE xception: Batch update row cou
    nt wrong: 0; nested exception is net.sf.hibernate.HibernateException: Batch upda
    te row count wrong: 0
    Failed to insert entry!
    com.myapp.exceptions.PersistenceException: org.springframework.orm.hibernate.Hib
    ernateSystemException: Batch update row count wrong: 0; nested exception is net.
    sf.hibernate.HibernateException: Batch update row count wrong: 0
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 20.703 sec


    my applicationContext is as follow

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <!--
    - Application context definition for JPetStore's business layer.
    - Contains bean references to the transaction manager and to the DAOs in
    - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
    -->
    <beans>

    <!-- ========================= GENERAL DEFINITIONS ========================= -->



    <!-- Configurer that replaces ${...} placeholders with values from properties files -->
    <!-- (in this case, mail and JDBC related properties) -->

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">

    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/menagerie?autoReconnect=true</value>
    </property>

    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>mypass</value>
    </property>
    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>com/myapp/hibernate/HibernateEntry.hbm.xml</value>
    <value>com/myapp/hibernate/HibernateExpenseType.hbm.xml</value>
    <value>com/myapp/hibernate/HibernateUser.hbm.xml</value>
    </list>
    </property>
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="show_sql">true</prop>
    <prop key="use_outer_join">true</prop>

    <prop key="hibernate.hbm2ddl.auto">create</prop>
    <prop key="cache.use_query_cache">true</prop>
    </props>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="persistenceManager" class="com.myapp.hibernate.HibernatePersistenceMan ager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    </beans>


    and the code that actually does the insert is following (the DAO extends HibernateDAOSupport)....

    public void insert(Entry data) throws PersistenceException {
    try {
    _log.error("INSERTING ENTRY...:" + data.getDescription());

    getHibernateTemplate().saveOrUpdate(data);
    } catch(Exception e) {
    _log.error("Exception in creating HibernateEntry\n" + e);

    throw new PersistenceException(e);
    }
    }


    i have tried with both saveOrUpdateCopy and save only, but as result my whole table data got deleted...

    'anyone can point me to what i am doing wrong?

    thanx in advance and regards
    marco

  • #2
    Have a look at the following article, this should help you a bit I hope.

    Alef

    http://www.hibernate.org/74.html#A26

    Comment


    • #3
      Hello Alef,
      thanx for the reply, i'll try it.
      just one more little question: any reasons why when i was using Hibernate OUTSIDE spring, i never got that error?

      thanx in advance and regards
      marco

      Comment


      • #4
        Hello Alef,
        that helped, however i cannot understand why when i update one object and then save it using saveOrUpdate, hibernate deletes my whole table....
        both in inserting and updating i am using saveOrUpdate........
        any clues on why that is happening??

        regards
        marco

        Comment

        Working...
        X