I'm attempting to set up a bi-directional, one-to-many relationship.
On the "many" side, the primary key is a composite consisting of
all the columns in the table. One of the columns is a foreign key.
When I try an insert, the SQL for the 'one' is fine and generates a key,
but the SQL to store the 'many' fails to assign the foreign key and so it
tries to insert the records with NULLs for the foreign keys.
Hibernate version: 3.2.1.ga
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.biz.model.NoteDetail"
table="NoteDetail" schema="dbo" lazy="false">
<id name="logNoteID" type="integer">
<column name="LogNoteID" />
<generator class="identity" />
</id>
<property name="note" type="string">
<column name="Note" length="500" not-null="true" />
</property>
<set name="accounts" cascade="all" inverse="true">
<key column="logNoteID"/>
<one-to-many
class="com.biz.model.NoteAccount"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="com.biz.model.NoteAccount"
table="NoteAccount" schema="dbo" lazy="false">
<composite-id>
<key-property name="logNoteID" column="LogNoteID"/>
<key-property name="property" type="string"
column="Property" />
<key-property name="account" type="string"
column="Account"/>
</composite-id>
<many-to-one name="logNoteDetail" column="LogNoteID"
insert="false" update="false" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): Code:
NoteDetail logDetail = new NoteDetail();
logDetail.setNote("prefers his martinis shaken, not stirred");
Set<NoteAccount> set = new HashSet<NoteAccount>();
NoteAccount ln1 = new NoteAccount();
ln1.setProperty("206");
ln1.setAccount("456000");
ln1.setLogNoteDetail(logDetail);
set.add(ln1);
NoteAccount ln2 = new NoteAccount();
ln2.setProperty("206");
ln2.setAccount("456001");
ln2.setLogNoteDetail(logDetail);
set.add(ln2);
logDetail.setAccounts(set);
newLogNoteDAO.saveNewLogNote(logDetail);
Full stack trace of any exception that occurs:
05 Jun 2007 15:32:06,313 [http-8080-Processor24] DEBUG - org.hibernate.util.JDBCExceptionReporter.logExceptions(69) | could not insert: [com.biz.model.NoteAccount] [insert into dbo.NoteAccount (LogNoteID, Property, Account) values (?, ?, ?)]
java.sql.SQLException: Cannot insert the value NULL into column 'LogNoteID', table 'FRACTIONAL_SUPP_JIM.dbo.NoteAccount'; column does not allow nulls.
INSERT fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2197)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2610)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:654)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:624)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:307)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy36.saveNewLogNote(Unknown Source)
at com.biz.ui.note.NotesSearch.prerender(NotesSearch.java:182)
at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.prerender(ViewHandlerImpl.java:860)
at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:295)
at org.ajax4jsf.framework.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
at org.ajax4jsf.framework.ajax.AjaxViewHandler.renderView(AjaxViewHandler.java:229)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:221)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.doService(FacesServlet.java:131)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:104)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:96)
at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:220)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:81)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:217)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:191)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:90)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Name and version of the database you are using: SQL Server
The generated SQL (show_sql=true):
05 Jun 2007 15:32:05,344 [http-8080-Processor24] DEBUG - org.hibernate.jdbc.AbstractBatcher.log(393) |
insert
into
dbo.NoteDetail
(Note)
values
(?)
05 Jun 2007 15:32:05,375 [http-8080-Processor24] DEBUG - org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(1942) | Dehydrating entity:
[com.biz.model.NoteDetail#<null>]
05 Jun 2007 15:32:05,375 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding 'prefers his martinis shaken, not stirred' to parameter: 1
05 Jun 2007 15:32:05,375 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding 'prefers his martinis shaken, not stirred' to parameter: 1
05 Jun 2007 15:32:05,407 [http-8080-Processor24] DEBUG - org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(37) | Natively generated identity: 2
-AND-
05 Jun 2007 15:32:06,188 [http-8080-Processor24] DEBUG - org.hibernate.jdbc.AbstractBatcher.log(393) |
insert
into
dbo.NoteAccount
(LogNoteID, Property, Account)
values
(?, ?, ?)
05 Jun 2007 15:32:06,188 [http-8080-Processor24] DEBUG - org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(476) | preparing statement
05 Jun 2007 15:32:06,219 [http-8080-Processor24] DEBUG - org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(1942) | Dehydrating entity:
[com.biz.model.NoteAccount#component[logNoteID,property,account]{account=456001, property=26, logNoteID=null}]
05 Jun 2007 15:32:06,235 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(73) | binding null to parameter: 1
05 Jun 2007 15:32:06,235 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(73) | binding null to parameter: 1
05 Jun 2007 15:32:06,250 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding '26' to parameter: 2
05 Jun 2007 15:32:06,250 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding '26' to parameter: 2
05 Jun 2007 15:32:06,250 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding '456001' to parameter: 3
05 Jun 2007 15:32:06,250 [http-8080-Processor24] DEBUG - org.hibernate.type.NullableType.nullSafeSet(80) | binding '456001' to parameter: 3