-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Saving association table before saving the main entity table
PostPosted: Wed May 16, 2007 3:44 pm 
Newbie

Joined: Wed May 16, 2007 3:32 pm
Posts: 1
I have a many-to-many association.
Organization -> OrgStaff -> Staff. An organization can have many staff members and a staff member can be involved in many organizations.

When i try to add a new organization, it first tries to insert into the association 'OrgStaff' and thus causes the error

org.springframework.dao.DataIntegrityViolationException: could not insert:



I am saving using hibernatetemplate

public void save(Organization organization) {
logger.info("Saving organization! - Hibernate3");

//i dont know if this is required, but by adding this
// editing works fine, otherwise it was complaining about
// NonUniqueObjectException
getHibernateTemplate().flush();
//getHibernateTemplate().saveOrUpdate(organization);
getHibernateTemplate().merge(organization);
}

What am I doing wrong?

Hibernate version: 3.2.2

Mapping documents:

<class name="com.tsp.crms.domain.model.Organization" table="Organization">
<id name="organizationId" type="long">
<generator class="native"/>
</id>
<property name="organizationName" />
<property name="contactName" />
<property name="contactPerson" />
<property name="contactPhone" />
<property name="contactAddress" />
<property name="zipCode" />
<set name="orgStaff"
cascade="all, delete-orphan"
inverse="false"
fetch="subselect">
<key column="OrganizationId" not-null="true"/>
<one-to-many class="com.tsp.crms.domain.model.OrgStaff"/>
</set>
</class>

<class name="com.tsp.crms.domain.model.OrgStaff" table="OrgStaff" mutable="false">

<!-- A composite primary key, assigned by the application -->
<composite-id name="id" class="com.tsp.crms.domain.model.OrgStaff$Id">
<key-property name="organizationId" column="organizationId"/>
<key-property name="staffId" column="staffId"/>
</composite-id>

<!-- Regular property -->
<property name="rank"
column="rank"
type="string"
not-null="false"/>

<!-- Alternatively, a many-to-one association to User:
<many-to-one name="addedByUser"
column="ADDED_BY_USER_ID"
class="User"
not-null="true"
foreign-key="FK_ADDED_BY_USER_ID"/>
-->

<!-- Read-only association property -->
<many-to-one name="staff"
column="staffId"
not-null="true"
insert="false"
update="false"
foreign-key="FK_orgstaff_staff_ID"/>

<!-- Read-only association property -->
<many-to-one name="organization"
column="organizationId"
not-null="true"
insert="false"
update="false"
foreign-key="FK_orgstaff_organization_ID"/>
</class>

<class name="com.tsp.crms.domain.model.Staff" table="Staff">
<id name="staffId" type="long">
<generator class="increment"/>
</id>
<property name="firstName" />
<property name="lastName" />
<property name="loginID" />
<property name="password" />
<!-- We use a one-to-many association to express the relationship
to a set of categories. There is an intermediate entity class,
CategorizedItem, which effectively makes this a many-to-many
association between Item and Category.
-->
<set name="orgStaff"
cascade="all, delete-orphan"
inverse="true">
<key column="StaffId" not-null="true"/>
<one-to-many class="com.tsp.crms.domain.model.OrgStaff"/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

org.hibernate.exception.ConstraintViolationException: could not insert: [com.tsp.crms.domain.model.OrgStaff]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2263)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:806)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:804)
com.tsp.crms.domain.dao.hibernate.OrganizationDaoHibernate.save(OrganizationDaoHibernate.java:46)
com.tsp.crms.service.impl.OrganizationManagerImpl.saveOrganization(OrganizationManagerImpl.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:299)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy53.saveOrganization(Unknown Source)
com.tsp.crms.web.AddOrganizationFormController.onSubmit(AddOrganizationFormController.java:92)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:250)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:819)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:754)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:399)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:364)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:81)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:217)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:229)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:183)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
Name and version of the database you are using:

MS SQL Server 2005


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.