-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to avoid org.hibernate.TransientObjectException
PostPosted: Wed Feb 11, 2009 11:24 pm 
Newbie

Joined: Wed Feb 11, 2009 1:45 am
Posts: 1
How to avoid org.hibernate.TransientObjectException when saving not null object with null id.

My Hibernate version: 3.0

There is an entities : Icident, place, station.
I'm trying to save instance of Incident object to the database. The object i saving contains a place, and a place contains station. Here is the mapping for classes i use (Incident and Station, the Place has no mapping because in my case it's a component of incident).

Mapping documents:

<hibernate-mapping package="org.incidents.model">
<class name="Incident" table="incident" lazy="false">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="happendDate" column="hap_date"></property>
<component name="place">
<many-to-one column="id_reg" name="region" class="org.incidents.model.place.Region"
not-found="exception" cascade="none" insert="true" update="true" lazy="false">
</many-to-one>
<many-to-one name="station" column="id_st" class="org.incidents.model.place.Station"
not-found="exception" cascade="none" insert="true" update="true" not-null="false" >
</many-to-one>
</component>
</class>
</hibernate-mapping>

<hibernate-mapping package="org.incidents.model.place">
<class name="Station" table="mn_tzsn" mutable="false" lazy="false">
<id name="id" column="kdzs">
<generator class="assigned"></generator>
</id>
<property name="name" column="nmzs"></property>
<property name="receptacle" column="kdrg"></property>
</class>
</hibernate-mapping>


Here is the java classes:
public class Incident {
private long id;
private Date happendDate;
private Place place;
}
public class Place {
private Region region;
private Station station;
}
public class Station implements Contents {
private Integer id;
private String name;
private int receptacle;
}
of course all classes have getters and setters for all properties.

so the problem is: i'm trying to save the Incident instace witch conains not empty (I mean not null) instance of Place, and a Place contains not empty instance of Station. BUT!!! The value of property "id" in the station is null. not the whole object station is null, but it's id. (name and receptacle may be not empty - it doesn't matter). So, when I'm saving (persist) the such Incident objetc to databese i get an exception like this:


Full stack trace of any exception that occurs:
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.incidents.model.place.Station
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:77)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:261)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1826)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2172)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:84)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:579)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:631)
at org.incidents.service.IncidentsService$$EnhancerByCGLIB$$9ddd3c60.saveIncident(<generated>)
at org.incidents.controller.EditIncidentController.onSubmit(EditIncidentController.java:84)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:859)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)


I know why this exception is thrown, it's because object with null identifier cann't be saved. And I don't want to save it. i want to hibernate just save my Incident to the database like it has Station=null (just clear's the "id_st" column in my table incident). In short - all i want in this case to hibernate "think's" that object with null identifier equal null object himself. Is that possible? I mean is any option in mapping or in hibernate properties what help's me "emulate" this behaviour?

beacuse then i trying to save Incident with station=null all work's perfectly just as i need.

Name and version of the database you are using:
Oracle 10


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 1:25 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
If you want hibernate to set the station field to null in your table then you should set null station in the Incident entity. But you can make hibernate to save the referenced transient objects automatically while saving an entity by putting cascade="persist, merge, save-update" in the many-to-one mappings.

_________________
Regards,
Litty Preeth


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