-->
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.  [ 3 posts ] 
Author Message
 Post subject: Foreign key violation with autoinc column
PostPosted: Wed Mar 14, 2007 10:44 am 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Hi, i have an interesting problem. I have two tables. Customer and customeraddress. They are splitted cause of performance.

The customer has a column called id and it is autoinc. Customeraddress has a column called customerid and this is just an integer (PK). The customer now has a foreign key with the id-column and the customerid of the customeraddress.

Now if i create a new customer with hibernate, i also have a customeraddress-objekt in here. the customer gets a new id, cause its autoinc, while saving it, but i get an exception, that the customeraddress-object can not be saved cause of an foreign key violation. Does anybody know, how to say hibernate, that he should use the customer id-column for the customeraddress customerid-column.

The exception
Code:
org.springframework.dao.DataIntegrityViolationException: could not insert: [de.nfranze.sf.model.Customer]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [de.nfranze.sf.model.Customer]
Caused by:
org.hibernate.exception.ConstraintViolationException: could not insert: [de.nfranze.sf.model.Customer]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2093)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2573)
   at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
   at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:679)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:676)
   at de.nfranze.sf.logic.dao.CustomerDAO.Save(CustomerDAO.java:38)
   at de.nfranze.sf.logic.manager.CustomerManager.Create(CustomerManager.java:24)
   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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:280)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
   at $Proxy8.Create(Unknown Source)
   at de.nfranze.sf.controller.RegisterWizardController.processFinish(RegisterWizardController.java:160)
   at org.springframework.web.servlet.mvc.AbstractWizardFormController.validatePagesAndFinish(AbstractWizardFormController.java:650)
   at org.springframework.web.servlet.mvc.AbstractWizardFormController.processFormSubmission(AbstractWizardFormController.java:490)
   at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:250)
   at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:820)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
   at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:138)
   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(Unknown Source)
Caused by: java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails (`sf/customer`, CONSTRAINT `FK_contact` FOREIGN KEY (`id`) REFERENCES `custcontact` (`customerid`))   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
   at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1129)
   at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:681)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1368)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1283)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1268)
   at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)


Does anybody know how to configure hibernate that he knows by its own that the foreign key datasetes need the same value as the customer id in the autoinc-column?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 11:40 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Use a foreign generator in the costumer address object : http://www.hibernate.org/hib_docs/v3/re ... -generator

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 11:52 am 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Ah, that sounds good. But i generate my hbm.xml-files with the hibernate tool. i have written a build.xml that uses the tool to generate the hbm.xml-files. Can you say me the tag i have to use to generate the generator?? (Nice sentence. If this works, you can tell me more about the credits.. :-))

Here is another information about it.
This is the code from my build.xml that generates the files.

Code:
<hibernate
               destination="${web.dir}/src"
               package="de.nfranze.sf.model"
               standardGeneratorScheme="native"
               javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"/>


And my hbm.xml files look like this

Code:
<class
    name="de.nfranze.sf.model.Customer"
    table="customer"
>

    <id
        name="id"
        type="java.lang.Integer"
        column="id"
    >
        <generator class="native" />
    </id>
...


How does i have to change the build.xml that always the right foreign-keys are generated?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.