Hi, sorry - I am sure this is such a basic question, but I just can't wrap my brain around it at the moment.
I have a class "DefaultConstructionNode" which contains another class "DefaultPrice" (actually two of them, and other things as well).
I want to map each class to its own table: CONSTRUCTION and PRICE respectively. And I want to have a foreign-key in the CONSTRUCTION table which points to the appropriate row in the PRICE table.
But I am having problems implementing this mapping. How do I map from one class to another like this, and ensure that the correct object is created and saved first? I have tried <many-to-one>, but really it should be <one-to-one>?
The mapping document below is a little more complicated than I explained, because there is inheritance involved for example, but the basic area of the problem is described above (classes DefaultConstructionNode and DefaultPrice).
Thanks for any help.
Peter
Hibernate version:
2.1.6
Mapping documents:
<hibernate-mapping>
<class name="dk.ciber.danbro.common.impl.AbstractBase" table="BASE" >
<cache usage="read-write" />
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<joined-subclass name="dk.ciber.danbro.price.impl.DefaultPrice" table="PRICE">
<key column="ID" />
<property name="amount" column="AMOUNT" type="double" />
<property name="remark" column="REMARK" type="string" />
<property name="date" column="PRICE_DATE" type="date" />
</joined-subclass>
<joined-subclass name="dk.ciber.danbro.common.impl.AbstractNode" table="NODE">
<key column="ID" />
<set name="children" table="NODE">
<cache usage="read-write" />
<key column="PARENT_ID"/>
<one-to-many class="dk.ciber.danbro.common.impl.AbstractNode"/>
</set>
<many-to-one name="parent" column="PARENT_ID" class="dk.ciber.danbro.common.impl.AbstractNode" />
<joined-subclass name="dk.ciber.danbro.facility.impl.DefaultFacilityNode" table="FACILITY">
<key column="ID" />
<property name="name" column="NAME" type="string" />
<property name="number" column="FACILITY_NUMBER" type="long" />
<joined-subclass name="dk.ciber.danbro.facility.impl.DefaultElementNode" table="ELEMENT">
<key column="ID" />
</joined-subclass>
<joined-subclass name="dk.ciber.danbro.facility.impl.DefaultConstructionNode" table="CONSTRUCTION">
<key column="ID" />
<property name="description" column="GENERAL_INFORMATION" type="string" />
<property name="registrationNumber" column="REGISTRATION_NUMBER" type="integer" />
<property name="identification" column="IDENTIFICATION" type="string" />
<property name="designation" column="DESIGNATION" type="string" />
<property name="abbreviation" column="ABBREVIATION" type="string" />
<many-to-one name="constructionPrice" column="CONSTRUCTION_PRICE" class="dk.ciber.danbro.price.impl.DefaultPrice" />
<many-to-one name="purchasePrice" column="PURCHASE_PRICE" class="dk.ciber.danbro.price.impl.DefaultPrice" />
</joined-subclass>
<joined-subclass name="dk.ciber.danbro.facility.impl.DefaultNameNode" table="VD">
<key column="ID" />
</joined-subclass>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
2004-09-14 14:23:27,734 ERROR [net.sf.hibernate.impl.SessionImpl] - <Could not synchronize database state with session>
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: dk.ciber.danbro.price.impl.DefaultPrice
at net.sf.hibernate.impl.SessionImpl.throwTransientObjectException(SessionImpl.java:2768)
at net.sf.hibernate.impl.SessionImpl.getEntityIdentifierIfNotUnsaved(SessionImpl.java:2760)
at net.sf.hibernate.type.EntityType.getIdentifier(EntityType.java:66)
at net.sf.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:47)
at net.sf.hibernate.persister.NormalizedEntityPersister.dehydrate(NormalizedEntityPersister.java:377)
at net.sf.hibernate.persister.NormalizedEntityPersister.dehydrate(NormalizedEntityPersister.java:360)
at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:690)
at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:670)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:214)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:177)
at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:312)
at dk.ciber.danbro.common.impl.HibernateDao.store(HibernateDao.java:78)
at dk.ciber.danbro.facility.impl.DefaultFacilityManager.storeConstruction(DefaultFacilityManager.java:71)
at dk.ciber.danbro.facility.web.ConstructionNodeEditController.onSubmit(ConstructionNodeEditController.java:52)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:295)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:270)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:222)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:237)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:121)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:485)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:322)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:
Hypersonic
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|