2014-03-07 200 views
1

大家好,感謝您的時間。我遇到了一些自動生成java.util.UUID的麻煩。Hibernate IdentifierGenerationException:無法識別的id類型:uuid-binary

考慮下面的實體:

@javax.persistence.Entity 
@javax.persistence.Table(name="clients") 
public class Client { 
    @javax.persistence.GeneratedValue 
    @javax.persistence.Id 
    private UUID id; 

    public UUID getId() { 
     return id; 
    } 
} 

當試圖堅持一個新的實例,我得到以下堆棧跟蹤:

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: unrecognized id type : uuid-binary -> java.util.UUID 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:257) 
at com.sun.proxy.$Proxy49.persist(Unknown Source) 
at com.shorecg.artemis.model.repository.impl.jpa.JpaRepository.save(JpaRepository.java:116) 
at com.shorecg.artemis.model.repository.impl.jpa.JpaRepository$$FastClassByCGLIB$$1a43e5e0.invoke(<generated>) 
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646) 
at com.shorecg.artemis.model.repository.impl.jpa.authn.JpaClientRepository$$EnhancerByCGLIB$$e44cf7a.save(<generated>) 
at com.shorecg.artemis.server.controllers.Client.postJson(Client.java:71) 
at com.shorecg.artemis.server.controllers.Client$$FastClassByCGLIB$$8f5a18aa.invoke(<generated>) 
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646) 
at com.shorecg.artemis.server.controllers.Client$$EnhancerByCGLIB$$b3bf8521.postJson(<generated>) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214) 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:696) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1568) 
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:164) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1539) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) 
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1548) 
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:524) 
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) 
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568) 
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1110) 
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:453) 
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183) 
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1044) 
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199) 
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109) 
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
at org.eclipse.jetty.server.Server.handle(Server.java:459) 
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:280) 
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229) 
at org.eclipse.jetty.io.AbstractConnection$1.run(AbstractConnection.java:505) 
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607) 
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536) 
at java.lang.Thread.run(Thread.java:724) 

我見過很多的解決方案,包括使用@GenericGenerator的。如果模型由供應商提供並且無法修改,那麼我怎麼才能糾正這種情況。我遇到了一些代碼片斷,這表明我可以通過SessionFactory註冊一個默認生成器,但我還沒有確定如何。

任何幫助,非常感謝。

編輯:

我做了一些通過軌跡示來源挖,我來到這個節org.hibernate.id.IdentifierGeneratorHelper:

Class clazz = type.getReturnedClass(); 
if (rs.getMetaData().getColumnCount() == 1) { 
    if (clazz == Long.class) { 
     return rs.getLong(1); 
    } 
    else if (clazz == Integer.class) { 
     return rs.getInt(1); 
    } 
    else if (clazz == Short.class) { 
     return rs.getShort(1); 
    } 
    else if (clazz == String.class) { 
     return rs.getString(1); 
    } 
    else if (clazz == BigInteger.class) { 
     return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY).toBigInteger(); 
    } 
    else if (clazz == BigDecimal.class) { 
     return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY); 
    } 
    else { 
     throw new IdentifierGenerationException(
      "unrecognized id type : " + type.getName() + " -> " + clazz.getName() 
     ); 
    } 
} 

一目瞭然,除非我錯過鏈中更高級的東西,否則似乎沒有辦法將除原始元素以外的其他東西作爲生成的ID返回。

此外編輯:

在數據庫中的列類型是不正確的。修復它並沒有糾正這個問題。但是我注意到,如果我使用java.util.UUID.randomUUID自己生成UUID,那麼Hibernate會正確地保存ID。在這一點上,我正在推進應用程序生成的UUID,但我想知道爲什麼我一直有這個問題。另外,我願意考慮爲什麼我不打算讓Hibernate生成uuids。

回答

0

對於更好的選擇,我現在正在生成我的模型中的UUID。不是我想要的,但它有效。作爲一個側面說明,我不得不用@Column(length = 16)註釋我的UUID字段,否則零填充會搞砸比較。

0

所以線索來自幾行org.hibernate.id.IdentifierGeneratorHelper上面的代碼中你報:

if (ResultSetIdentifierConsumer.class.isInstance(type)) { 
     return ((ResultSetIdentifierConsumer) type).consumeIdentifier(rs); 
    } 

在你的情況,你可以創建一個實現用戶類型和ResultSetIdentifierConsumer一個自定義的ID類型。見this tutorial