SmartGWT: Issue with many to many relationships, using hibernate annotations
===

I am facing a strange issue working with many to many relationships with incorrect values being passed from the front end. 

Let me explain the problem:

I have a many to many relationship between "USER" and "DEPARTMENT" tables(A user can work in different departments). So, there is an intermediate table USER_DEPARTMENT.

I have ds.xml files defined which are as follows:

    <DataSource
	ID="user"
	serverType="hibernate"
	beanClassName="com.myapp.gwt.platform.server.User"
	schemaBean="com.myapp.gwt.platform.server.User"
	dropExtraFields="true">

	<fields>
		<field name="id" type="sequence" hidden="true" primaryKey="true" />
		
        <field name="departments" hidden="true" multiple="true" type="department"
           foreignKey="department.id"  />
		   
	</fields>

	<operationBindings>
		<operationBinding operationType="add">
			<serverObject lookupStyle="spring" bean="UserDMIHandler" methodName="executeAdd" />
		</operationBinding>
		<operationBinding operationType="update" operationId="attUpdate">
			<serverObject lookupStyle="spring" bean="UserDMIHandler" methodName="executeUpdate" />
		</operationBinding>
		<operationBinding operationType="remove" >
			<serverObject lookupStyle="spring" bean="UserDMIHandler" methodName="executeRemove" />
		</operationBinding>
		<operationBinding operationType="fetch">
			<serverObject lookupStyle="spring" bean="UserDMIHandler" methodName="executeFetch" />
		</operationBinding>
	</operationBindings>
</DataSource>

and 

    <DataSource
	ID="department"
	serverType="hibernate"
	beanClassName="com.myapp.gwt.platform.server.Department"
	schemaBean="com.myapp.gwt.platform.server.Department"
	dropExtraFields="true">

	<fields>
		<field name="id" type="sequence" hidden="true" primaryKey="true" />		   
	</fields>

	<operationBindings>
		<operationBinding operationType="add">
			<serverObject lookupStyle="spring" bean="DepartmentDMIHandler" methodName="executeAdd" />
		</operationBinding>
		<operationBinding operationType="update" operationId="attUpdate">
			<serverObject lookupStyle="spring" bean="DepartmentDMIHandler" methodName="executeUpdate" />
		</operationBinding>
		<operationBinding operationType="remove" >
			<serverObject lookupStyle="spring" bean="DepartmentDMIHandler" methodName="executeRemove" />
		</operationBinding>
		<operationBinding operationType="fetch">
			<serverObject lookupStyle="spring" bean="DepartmentDMIHandler" methodName="executeFetch" />
		</operationBinding>
	</operationBindings>
</DataSource>

I am using a DynamicForm in the front end to display a user's department in a SelectItem. The SelectItem is populated with all the departments in the database. Whenever a user is edited via the front end, I get an exception:

    org.hibernate.AssertionFailure: null id in com.myapp.gwt.platform.server.Department entry (don't flush the Session after an exception occurs)

I see a strange SQL query being executed on the console whereby the application tries to fetch a DEPARTMENT based on DEPARTMENT_ID as NULL. Like the one below:

    Hibernate: select department0_.DEPARTMENT_ID as DEPARTMENT1_133_, department0_.DEPARTMENT_DESC as DEPARTMENT7_133_ from DEPARTMENT department0_ where department0_.DEPARTMENT_ID=?

And just below that I am printing the department object as:

    Department[departmentDescription=null, id=null, getId()=null, hashCode()=31, getClass()=class com.myapp.gwt.platform.server.Department, toString()=com.myapp.gwt.platform.server.Department@1f]

Just below this I am getting the exception mentioned above, which is:

    org.hibernate.AssertionFailure: null id in com.myapp.gwt.platform.server.Department entry (don't flush the Session after an exception occurs)
	at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
	at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
	at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
	at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
	at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
	at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
	at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
	at com.isomorphic.hibernate.HibernateDataSource.setRelationFieldValue(HibernateDataSource.java:5378)
	at com.isomorphic.datasource.DataSource.setProperties(DataSource.java:1394)
	at com.isomorphic.datasource.DataSource.setProperties(DataSource.java:1354)
	at com.isomorphic.hibernate.HibernateDataSource.processRequest(HibernateDataSource.java:1402)
	at com.isomorphic.hibernate.HibernateDataSource.executeUpdate(HibernateDataSource.java:590)
	at com.isomorphic.datasource.DataSource.execute(DataSource.java:1295)
	at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:725)
	at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:658)
	at com.isomorphic.application.AppBase.execute(AppBase.java:491)
	at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1948)
	at com.myapp.gwt.platform.server.BaseDMIHandler.executeUpdate(BaseDMIHandler.java:163)
	at com.myapp.gwt.platform.server.UserDMIHandler.executeUpdate(UserDMIHandler.java:342)
	at com.myapp.gwt.platform.server.UserDMIHandler$$FastClassByCGLIB$$76d55d66.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
	at com.myapp.gwt.platform.server.UserDMIHandler$$EnhancerByCGLIB$$a14fb280.executeUpdate(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.isomorphic.base.Reflection.adaptArgsAndInvoke(Reflection.java:950)
	at com.isomorphic.datasource.DataSourceDMI.execute(DataSourceDMI.java:386)
	at com.isomorphic.datasource.DataSourceDMI.execute(DataSourceDMI.java:64)
	at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1945)
	at com.isomorphic.servlet.IDACall.handleDSRequest(IDACall.java:199)
	at com.isomorphic.servlet.IDACall.processRPCTransaction(IDACall.java:156)
	at com.isomorphic.servlet.IDACall.processRequest(IDACall.java:121)
	at com.isomorphic.servlet.IDACall.doPost(IDACall.java:73)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at com.isomorphic.servlet.BaseServlet.service(BaseServlet.java:152)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1097)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
	at com.isomorphic.servlet.CompressionFilter.doFilter(CompressionFilter.java:259)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.Server.handle(Server.java:324)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
	at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

On the front end, the SelectItem is populated correctly with all the department descriptions. This exception appears when I try to update the user with a new department. 

On the view, I am adding the SelectItem to a dynamic form as below:

    department = new SelectItem("departments");
		department.setRequired(true);
		department.setTitle("Department");
		department.setDisplayField("departmentDescription");
		department.setValueField("id");
		department.setOptionDataSource(DataSource.get("department"));

The display and value fields are as specified in the entities.
Have been struggling with the issue for over a day now. Not sure what the problem is.
Any help would be appreciated.