Hi People,
I'm using hibernate 3.6
I have one problem with using trigger to generate a pk value with hibernate.
Well, I implement the GenericGenerator based on this thread http://forum.hibernate.org/viewtopic.php?p=2427710
Code:
package br.com.optimuslabs.infra.persistence;
...
public class TriggerAssignedIdentityGenerator extends AbstractPostInsertGenerator {
private static final String RETURN_PREFIX = " RETURNING ";
private Delegate delegate = null;
public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate(
final PostInsertIdentityPersister persister, final Dialect dialect, final boolean isGetGeneratedKeysEnabled)
throws HibernateException {
delegate = new Delegate(persister, dialect);
return delegate;
}
private static final class Delegate extends AbstractReturningDelegate {
private final Dialect dialect;
private final String[] keyColumns;
private final String sqlSuffix;
public Delegate(final PostInsertIdentityPersister persister, final Dialect dialect) {
super(persister);
this.dialect = dialect;
this.keyColumns = getPersister().getRootTableKeyColumnNames();
if (keyColumns.length > 1) {
throw new HibernateException("TriggerAssignedIdentityGenerator cannot be used with multi-column keys");
}
sqlSuffix = new StringBuffer(26).append(RETURN_PREFIX).append(keyColumns[0]).toString();
}
public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {
return new NoCommentsInsert(dialect);
}
@Override
protected PreparedStatement prepare(String insertSQL, final SessionImplementor session) throws SQLException {
insertSQL += sqlSuffix;
PreparedStatement pstmt = session.getBatcher().prepareStatement(insertSQL, keyColumns);
return pstmt;
}
@Override
protected Serializable executeAndExtract(final PreparedStatement insert) throws SQLException {
if (insert.executeUpdate() > 0) {
ResultSet resultSet = insert.getResultSet();
if (resultSet.next()) {
return new Integer(resultSet.getInt(1));
}
}
throw new SQLException("Returned no id in resultset");
}
}
}
And I obtain this exception
Code:
}: br.com.optimuslabs.infra.exceptions.OptimusMessageException: could not set a field value by reflection setter of br.com.optimuslabs.entity.local.Participante.cod
at br.com.optimuslabs.service.importacao.governo.sintegra.SintegraImportacaoService.upload(SintegraImportacaoService.java:199) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_21]
at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_21]
at org.granite.messaging.service.ServiceInvocationContext.invoke(ServiceInvocationContext.java:71) [granite-core-2.3.2.GA.jar:]
at org.granite.messaging.service.security.AbstractSecurityService.endAuthorization(AbstractSecurityService.java:77) [granite-core-2.3.2.GA.jar:]
at org.granite.spring.security.SpringSecurity3Service.authorize(SpringSecurity3Service.java:263) [granite-spring-2.3.2.GA.jar:]
at org.granite.messaging.service.ServiceInvoker.invoke(ServiceInvoker.java:214) [granite-core-2.3.2.GA.jar:]
at org.granite.messaging.amf.process.AMF3MessageProcessor.processRemotingMessage(AMF3MessageProcessor.java:136) [granite-core-2.3.2.GA.jar:]
at org.granite.messaging.amf.process.AMF3MessageProcessor.process(AMF3MessageProcessor.java:59) [granite-core-2.3.2.GA.jar:]
at org.granite.messaging.amf.process.AMF0MessageProcessor.process(AMF0MessageProcessor.java:78) [granite-core-2.3.2.GA.jar:]
at org.granite.messaging.webapp.AMFEndpoint.service(AMFEndpoint.java:71) [granite-core-2.3.2.GA.jar:]
at org.granite.spring.FlexFilter.handle(FlexFilter.java:245) [granite-spring-2.3.2.GA.jar:]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_21]
Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of br.com.optimuslabs.entity.local.Participante.cod
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:439) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:122) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_21]
at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_21]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at com.sun.proxy.$Proxy37.save(Unknown Source)
at br.com.optimuslabs.persistence.GenericDao.save(GenericDao.java:84) [optimus-java-common-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.GenericService.add(GenericService.java:108) [optimus-java-common-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.fiscal.ParticipanteService.add(ParticipanteService.java:69) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.fiscal.ParticipanteService$$FastClassByCGLIB$$db1bf48a.invoke(<generated>) [cglib-2.2.jar:]
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) [cglib-2.2.jar:]
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at br.com.optimuslabs.service.fiscal.ParticipanteService$$EnhancerByCGLIB$$3b901e8d.add(<generated>) [cglib-2.2.jar:]
at br.com.optimuslabs.service.importacao.governo.sintegra.SintegraImportacaoService.validateParticipante(SintegraImportacaoService.java:1381) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.importacao.governo.sintegra.SintegraImportacaoService.parseRegistro70(SintegraImportacaoService.java:1113) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.importacao.governo.sintegra.SintegraImportacaoService.parseDTO(SintegraImportacaoService.java:301) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
at br.com.optimuslabs.service.importacao.governo.sintegra.SintegraImportacaoService.upload(SintegraImportacaoService.java:196) [optimus-java-contabil-1.0-SNAPSHOT.jar:]
... 34 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field br.com.optimuslabs.entity.local.Participante.cod to org.hibernate.id.IdentifierGeneratorHelper$2
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [rt.jar:1.7.0_21]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [rt.jar:1.7.0_21]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) [rt.jar:1.7.0_21]
at java.lang.reflect.Field.set(Field.java:680) [rt.jar:1.7.0_21]
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139) [hibernate-core-3.6.9.Final.jar:3.6.9.Final]
... 72 more
I think my problem is on this part. This is implemented on superclass(AbstractPostInsertGenerator)
Code:
public Serializable generate(SessionImplementor s, Object obj) {
return IdentifierGeneratorHelper.POST_INSERT_INDICATOR;
}
I dont have certain what is correct to fixing this. The delegate class is no callable.
Or existing another solution to this problem.
Many Thanks