title: Hibernate: "Could not synchronize database state with session" on flush
body: <p>I am receiving the "Could not synchronize database state with session" when I attempt a flush in my unit test below.</p>
  
  <p>Can anyone advise what the problem is as I am completely lost? I have tried just creating a RuleDefinition without any of its attributes set and that doesn't work either.</p>
  
  <p>Thanks</p>
  
  <p>This is a snippet of my unit test which extends AbstractTransactionalDataSourceSpringContextTests</p>
  
  <pre><code>@Test
  public void testCreateRule() {
      RuleDefinition ruleReturned = (RuleDefinition) hibernateRuleDefinitionDao.findRule(1);
      RuleDefinition newRule = new RuleDefinition();
      newRule.setCurrentState("ACTIVE");
      newRule.setAttribute(ruleReturned.getSecondaryAttribute());
      newRule.setSecondaryAttribute(ruleReturned.getAttribute());
      newRule.setOperator(ruleReturned.getOperator());
      newRule.setPrecedence(4);
      hibernateRuleDefinitionDao.createRule(newRule);
      // Exception is thrown after the flush
      hibernateTemplate.flush();   
  }
  </code></pre>
  
  <p>The DAO method simply calls a save operation. This is my mapping file</p>
  
  <pre><code>&lt;class name="abc.def.rules.RuleDefinition" table="REFDATA.CONFIG_RULE_DEFINITION"&gt;
      &lt;id name="ruleId" column="RULE_ID"&gt;
          &lt;generator class="increment"/&gt;
      &lt;/id&gt;
      &lt;many-to-one name="attribute" cascade="none" class="abc.def.rules.ConfigAttribute" lazy="false" column="CONFIG_ATTR_ID"/&gt;
      &lt;many-to-one name="operator" cascade="none" class="abc.def.rules.Operator" lazy="false"&gt;
          &lt;column name="OPRTR_VAL"/&gt;
          &lt;column name="OPRTR_VAL_DATA_TYP"/&gt;
      &lt;/many-to-one&gt;
      &lt;many-to-one name="secondaryAttribute" cascade="none" class="abc.def.rules.ConfigAttribute" lazy="false" column="CONFIG_ATTR_ID_2" not-null="false"/&gt;
      &lt;property name="operand" column="OPRND_VAL" type="string"/&gt;
      &lt;property name="trueAction" column="TRUE_ACTN" type="string"/&gt;
      &lt;property name="falseAction" column="FALSE_ACTN" type="string"/&gt;
      &lt;property name="precedence" column="RULE_ORD_SEQ" type="int" not-null="true"/&gt;
      &lt;property name="currentState" column="RULE_STAT" type="string"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>This is a snippet of my Spring configuration file</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="org.hsqldb.jdbcDriver" /&gt;
      &lt;property name="url" value="jdbc:hsqldb:mem:testdb" /&gt;
      &lt;property name="username" value="sa" /&gt;
      &lt;property name="password" value="" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocations"&gt;
          &lt;list&gt;
              &lt;value&gt;classpath:abc\def\hibernate-reference.cfg.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;
                  org.hibernate.dialect.HSQLDialect
              &lt;/prop&gt;
              &lt;prop key="hibernate.cache.provider_class"&gt;
                  org.hibernate.cache.NoCacheProvider
              &lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;
                  false
              &lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>