Uploaded image for project: 'Spring Data JDBC'
  1. Spring Data JDBC
  2. DATAJDBC-229

Add java.util.UUID into simpleTypeHolder

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.0 M3 (Lovelace)
    • Fix Version/s: 1.0 RC2 (Lovelace)
    • Component/s: Core
    • Labels:
      None

      Description

      I think java.util.UUID is pretty common as an ID column.
      Currently, UUID is regarded as a referenced entity and the following exception occurs when UUID is used in an entity class:

      Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO uuid (most_sig_bits, least_sig_bits, tweets) VALUES (?, ?, ?)]; nested exception is org.h2.jdbc.JdbcSQLException: Table "UUID" not found; SQL statement:
      INSERT INTO uuid (most_sig_bits, least_sig_bits, tweets) VALUES (?, ?, ?)
      

      The full stack trace follows:

      org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute Insert for instance bc8616b3-6e4b-44c4-bc6b-44e114a6dbd4 of type class java.util.UUID with path uuid
      
      	at org.springframework.data.relational.core.conversion.DbAction.executeWith(DbAction.java:147)
      	at org.springframework.data.relational.core.conversion.AggregateChange.lambda$executeWith$0(AggregateChange.java:45)
      	at java.util.ArrayList.forEach(ArrayList.java:1257)
      	at org.springframework.data.relational.core.conversion.AggregateChange.executeWith(AggregateChange.java:45)
      	at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:85)
      	at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:47)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
      	at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:632)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:596)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:583)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:583)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
      	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
      	at com.sun.proxy.$Proxy75.save(Unknown Source)
      	at com.example.hajiboot2tweeterspringdatajdbc.TweetRepositoryTest.insertAndCount(TweetRepositoryTest.java:31)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
      	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
      Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO uuid (most_sig_bits, least_sig_bits, tweets) VALUES (?, ?, ?)]; nested exception is org.h2.jdbc.JdbcSQLException: Table "UUID" not found; SQL statement:
      INSERT INTO uuid (most_sig_bits, least_sig_bits, tweets) VALUES (?, ?, ?) [42102-197]
      	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234)
      	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
      	at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402)
      	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:620)
      	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:881)
      	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:345)
      	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:329)
      	at org.springframework.data.jdbc.core.DefaultDataAccessStrategy.insert(DefaultDataAccessStrategy.java:100)
      	at org.springframework.data.jdbc.core.DefaultJdbcInterpreter.interpret(DefaultJdbcInterpreter.java:53)
      	at org.springframework.data.relational.core.conversion.DbAction$Insert.doExecuteWith(DbAction.java:184)
      	at org.springframework.data.relational.core.conversion.DbAction.executeWith(DbAction.java:145)
      	... 61 more
      Caused by: org.h2.jdbc.JdbcSQLException: Table "UUID" not found; SQL statement:
      INSERT INTO uuid (most_sig_bits, least_sig_bits, tweets) VALUES (?, ?, ?) [42102-197]
      	at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
      	at org.h2.message.DbException.get(DbException.java:179)
      	at org.h2.message.DbException.get(DbException.java:155)
      	at org.h2.command.Parser.readTableOrView(Parser.java:5920)
      	at org.h2.command.Parser.readTableOrView(Parser.java:5897)
      	at org.h2.command.Parser.parseInsert(Parser.java:1220)
      	at org.h2.command.Parser.parsePrepared(Parser.java:431)
      	at org.h2.command.Parser.parse(Parser.java:335)
      	at org.h2.command.Parser.parse(Parser.java:307)
      	at org.h2.command.Parser.prepareCommand(Parser.java:278)
      	at org.h2.engine.Session.prepareLocal(Session.java:611)
      	at org.h2.engine.Session.prepareCommand(Session.java:549)
      	at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247)
      	at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
      	at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1166)
      	at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:226)
      	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
      	... 68 more
      

      As a workaround, adding UUID to simpleTypeHolder works

      @Component
      public class UuidAsIdTypeEnabler implements BeanPostProcessor {
      	@Override
      	public Object postProcessBeforeInitialization(Object bean, String beanName)
      			throws BeansException {
      		if (bean instanceof RelationalMappingContext) {
      			HashSet<Class<?>> CUSTOM_SIMPLE_TYPES = new HashSet<>(asList( //
      					BigDecimal.class, //
      					BigInteger.class, //
      					Temporal.class, //
      					UUID.class));
      			((RelationalMappingContext) bean)
      					.setSimpleTypeHolder(new SimpleTypeHolder(CUSTOM_SIMPLE_TYPES, true));
      		}
      		return bean;
      	}
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              schauder Jens Schauder
              Reporter:
              making Toshiaki Maki
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: