Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Question: What is the best way to convert a persistent object with many associations into a subclass with a table per concrete class implementation?
Hibernate version: 3.1.2
Mapping documents:
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.nkics.socialworkprn.swan.domain.Lead" table="LEAD">
<id name="id" type="int">
<column name="LEAD_ID" />
<generator class="native" />
</id>
<many-to-one name="primaryLeadLocation" class="com.nkics.socialworkprn.swan.domain.LeadLocation" fetch="select" lazy="false" cascade="persist,merge,save-update">
<column name="PRIMARY_LEAD_LOCATION_ID" />
</many-to-one>
<many-to-one name="leadType" class="com.nkics.socialworkprn.swan.domain.LeadType" fetch="select" lazy="false">
<column name="LEAD_TYPE_ID" not-null="true" />
</many-to-one>
<many-to-one name="office" class="com.nkics.socialworkprn.swan.domain.Office" fetch="select">
<column name="OFFICE_ID" not-null="true" />
</many-to-one>
<property name="active" type="boolean">
<column name="LEAD_ACTIVE" not-null="true" />
</property>
<property name="name" type="string">
<column name="LEAD_NAME" length="40" not-null="true" />
</property>
<property name="gsaSigned" type="timestamp">
<column name="LEAD_GSA_SIGNED" length="23" />
</property>
<property name="vendorAgreement" type="boolean">
<column name="LEAD_VENDOR_AGREEMENT" not-null="true" />
</property>
<property name="requirementsNote" type="string">
<column name="LEAD_REQUIREMENTS_NOTE" length="2000" />
</property>
<property name="generalNote" type="string">
<column name="LEAD_GENERAL_NOTE" length="2000" />
</property>
<property name="leadCreated" type="timestamp">
<column name="LEAD_CREATED" length="23" not-null="true" />
</property>
<property name="leadUpdated" type="timestamp">
<column name="LEAD_UPDATED" length="23" />
</property>
<set name="leadLocations" inverse="true" cascade="all,delete-orphan">
<key>
<column name="LEAD_ID" not-null="true" />
</key>
<one-to-many class="com.nkics.socialworkprn.swan.domain.LeadLocation" />
</set>
<joined-subclass name="com.nkics.socialworkprn.swan.domain.Client" table="CLIENT">
<key column="LEAD_ID" />
<property name="clientCreated" type="timestamp">
<column name="CLIENT_CREATED" length="23" not-null="true" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
Database Table Exerpt:Code:
CREATE TABLE LEAD(
LEAD_ID int IDENTITY(1,1),
LEAD_ACTIVE bit NOT NULL,
LEAD_NAME varchar(128) NOT NULL,
LEAD_GSA_SIGNED datetime NULL,
LEAD_VENDOR_AGREEMENT bit NOT NULL,
LEAD_REQUIREMENTS_NOTE varchar(2000) NULL,
LEAD_GENERAL_NOTE varchar(2000) NULL,
LEAD_CREATED datetime NOT NULL,
LEAD_UPDATED datetime NULL,
LEAD_TYPE_ID int NOT NULL,
OFFICE_ID int NOT NULL,
PRIMARY_LEAD_LOCATION_ID int NULL,
CONSTRAINT PK39 PRIMARY KEY NONCLUSTERED (LEAD_ID)
)
go
CREATE TABLE CLIENT(
LEAD_ID int NOT NULL,
CLIENT_CREATED datetime NOT NULL,
CONSTRAINT PK46 PRIMARY KEY NONCLUSTERED (LEAD_ID)
)
go
ALTER TABLE CLIENT ADD
FOREIGN KEY (LEAD_ID)
REFERENCES LEAD(LEAD_ID)
go
Code between sessionFactory.openSession() and session.close():I have tried several things, none of which works.
Code:
public Client convertLeadToClientById(int id)
{
logger.debug("converting lead#"+id+" to client");
try
{
SQLQuery sqlQuery = getSession().createSQLQuery("INSERT INTO CLIENT (LEAD_ID, CLIENT_CREATED) VALUES (?,?)");
sqlQuery.setInteger(0, id);
sqlQuery.setDate(1, new Date());
sqlQuery.executeUpdate();
Client client = (Client)getSession().get(Client.class, id);
logger.debug("converted lead#"+id+" to client successfully");
return client;
}
catch (RuntimeException e)
{
logger.error("failed to convert lead#"+id+" to client");
throw e;
}
}
Full stack trace of any exception that occurs:Code:
java.lang.UnsupportedOperationException: Update queries only supported through HQL
org.hibernate.impl.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:753)
com.nkics.socialworkprn.swan.persistence.hibernate.HibernateClientDao.convertLeadToClientById(HibernateClientDao.java:73)
com.nkics.socialworkprn.swan.service.defaults.DefaultLeadService.convertLeadToClientById(DefaultLeadService.java:486)
com.nkics.socialworkprn.swan.service.defaults.DefaultLeadService.updateLead(DefaultLeadService.java:473)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
$Proxy6.updateLead(Unknown Source)
com.nkics.socialworkprn.swan.mvc.client.UpdateLeadController.doSubmitAction(UpdateLeadController.java:314)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:382)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:356)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:249)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:79)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.ui.switchuser.SwitchUserProcessingFilter.doFilter(SwitchUserProcessingFilter.java:382)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:143)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:138)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:174)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.ui.basicauth.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:207)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:246)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:220)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:120)
Code between sessionFactory.openSession() and session.close():This doesn't work either...
Code:
public Client convertLeadToClientById(int id)
{
logger.debug("converting lead#"+id+" to client");
try
{
Client client = new Client();
client.setId(id);
getSession().saveOrUpdate(client);
logger.debug("converted lead#"+id+" to client successfully");
return client;
}
catch (RuntimeException e)
{
logger.error("failed to convert lead#"+id+" to client");
throw e;
}
}
Full stack trace of any exception that occurs:Code:
2006-08-09 18:39:47,001 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/swan].[swan]] - Servlet.service() for servlet swan threw exception
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.nkics.socialworkprn.swan.domain.Client#5]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.nkics.socialworkprn.swan.domain.Client#5]
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.nkics.socialworkprn.swan.domain.Client#5]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:629)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:258)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:588)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:576)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:568)
at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:661)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:658)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:654)
at com.nkics.socialworkprn.swan.persistence.hibernate.HibernateClientDao.convertLeadToClientById(HibernateClientDao.java:75)
at com.nkics.socialworkprn.swan.service.defaults.DefaultLeadService.convertLeadToClientById(DefaultLeadService.java:486)
at com.nkics.socialworkprn.swan.service.defaults.DefaultLeadService.updateLead(DefaultLeadService.java:473)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy6.updateLead(Unknown Source)
at com.nkics.socialworkprn.swan.mvc.client.UpdateLeadController.doSubmitAction(UpdateLeadController.java:314)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:382)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:356)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:249)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
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.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:79)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.switchuser.SwitchUserProcessingFilter.doFilter(SwitchUserProcessingFilter.java:382)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:143)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:138)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:174)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.basicauth.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:207)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:246)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:220)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:120)
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)
Please help. This is holding up my project. Thanks in advance.