Results 1 to 4 of 4

Thread: DataIntegrityViolationException during insert - Oracle

  1. #1
    Join Date
    Jul 2007
    Posts
    6

    Default DataIntegrityViolationException during insert - Oracle

    Hi all... hoping for some help here. I've read through several of the posts dealing with transactions, etc and have patterned my configs after those successful examples here in the forums.

    From my controller, I invoke a method call from my service class, which in turn calls a DAO object's createXXX method. Subsequently, I get a DataIntegrityViolationException when trying to insert a new 'Event' row with the following Oracle error:
    ORA-02291: integrity constraint (COMMUNITY.FK_GRP_EVT_EVT) violated - parent key not found

    When I step through the debug code, the DAO's getHibernateTemplate().save method returns successfully to the service class with the appropriate primary key value. Upon returning to the controller, however, is when I get this exception.

    Here's my config:

    Code:
    	<bean id="BaseTxProxy" lazy-init="false" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="TransactionManager"/>
    		</property>
    		<property name="transactionAttributes">
    			<props >
    				<prop key="create*">PROPAGATION_REQUIRED</prop>
    				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="EventService" parent="BaseTxProxy">
    		<property name="target">
    			<bean class="com.military.community.service.impl.EventServiceImpl">
    				<property name="eventDAO">
    					<ref bean="EventDAO" />
    				</property>
    				<property name="groupDAO">
    					<ref bean="GroupDAO" />
    				</property>
    			</bean>
    		</property>
    	</bean>
    
    	<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="CommunitySessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="EventDAO" class="com.military.community.dao.EventDAO">
    		<property name="sessionFactory">
    			<ref bean="CommunitySessionFactory" />
    		</property>
    		<property name="hibernateTemplate"> 
    			<ref bean="hibernateTemplate"/> 
    		</property>		
    	</bean>
    
    	<bean id="GroupDAO" class="com.military.community.dao.GroupDAO">
    		<property name="sessionFactory">
    			<ref bean="CommunitySessionFactory" />
    		</property>
    		<property name="hibernateTemplate"> 
    			<ref bean="hibernateTemplate"/> 
    		</property>		
    	</bean>
    
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory">
                <ref local="CommunitySessionFactory"/>
            </property>
            <property name="cacheQueries">
                <value>true</value>
            </property>
        </bean>
    Here's the method:
    Code:
    	public Long createEvent(EventBean bean, Long groupId) {
    		Long ret = null;
    		Event evt = new Event(bean);
    		try {
    			GroupEvent ge = new GroupEvent(groupDAO.findById(groupId), evt);
    			evt.getGroupEvents().add(ge);
    		} catch (RuntimeException e) {
    			// No group found. Ignore.
    		}
    		eventDAO.save(evt);
    		ret = evt.getId();
    		return ret;
    	}

    And the controller config:

    Code:
         <bean id="test" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
             <property name="interceptors">
                 <list>
                      <ref bean="openSessionInViewInterceptor"/>
                  </list>
              </property>
              <property name="urlMap">
                   <map>
                        <entry key="/test-group.htm">
                             <ref bean="testController" />
                        </entry>
                   </map>
              </property>
         </bean>
    
         <bean id="testController" class="com.military.community.controller.TestController">
    		<property name="eventService">
    			<ref bean="EventService" />
    		</property>
    		<property name="groupService">
    			<ref bean="GroupService" />
         </bean>
    Any pointers/help is appreciated. Thanks!

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    Looks like some problems related to Hibernate mappings. I would start investigating from this point of view.

    Try performing a more simple test in createEvent() - meaning, just create an Event entity, without considering a GroupEvent or EventBean.

  3. #3
    Join Date
    Jul 2007
    Posts
    6

    Default

    Thanks Andrei,

    I had just completed doing exactly what you suggested and discovered that the primary key value reported to Hibernate is always one less than the primary key Oracle is actually inserting in to the database

    <scratches head>

    I'm using a pretty basic sequence with a before-insert trigger to get the value in the first place.

  4. #4
    Join Date
    Jul 2007
    Posts
    6

    Default

    Solved.

    Didn't need the triggers at all. Blew them away and all works as expected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •