-->
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.  [ 4 posts ] 
Author Message
 Post subject: Composite ID w/ Foreign key: Exception Thrown
PostPosted: Tue Jun 05, 2007 4:05 pm 
Newbie

Joined: Tue Jun 05, 2007 10:29 am
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 9:49 am 
Newbie

Joined: Tue Jun 05, 2007 10:29 am
Posts: 2
I changed the NoteAccount ("many" side) mapping to the following and
it now works.


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.starwoodvo.sharcs.domain.model.NoteAccount" table="NoteAccount" schema="dbo" lazy="false">
      
        <composite-id>
          <key-many-to-one name="logNoteDetail" column="LogNoteID" />
         <key-property name="property" column="Property" />
         <key-property name="account" column="Account"/>
        </composite-id>
       
    </class>
</hibernate-mapping>


I tried using the "key-many-to-one" previously and got the same exception as in my original post. Obviously something is different, maybe in my table definition, maybe in my entity class. I've tried a million things to solve it, so I lost track of each version. Nonetheless, it now works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 3:24 pm 
Newbie

Joined: Fri Feb 13, 2009 3:22 pm
Posts: 1
I have exactly the same problem, could you post your solution?

Thanks much


Top
 Profile  
 
 Post subject: Re: Composite ID w/ Foreign key: Exception Thrown
PostPosted: Wed Jul 29, 2009 1:40 am 
Beginner
Beginner

Joined: Wed Aug 22, 2007 5:53 am
Posts: 38
I agree that with <key-many-to-one> your problem is solved but in "Java persistence with hibernate" book, it's written that
Quote:
However, it’s usually inconvenient to have an association in a composite identifier
class, so this approach isn’t recommended except in special circumstances. The
<key-many-to-one> construct also has limitations in queries: You can’t restrict a
query result in HQL or Criteria across a <key-many-to-one> join (although it’s
possible these features will be implemented in a later Hibernate version).


So the book refers to use the <many-to-one> with insert="false" and update="false"...
With this approach, can i get FK automatically assigned by hibernate ?


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

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.