title: Spring data jdbc error BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar, while trying to save simple Aggregate
body: <p>I've below domain model objects, with below DDL and spring version, when i try to save the complete aggregate (just two entities) spring-data below error.</p>
  <p>So I'm thinking that either i don't have my aggregates or my DDL defined correctly or hitting a blocker in spring-data-jdbc.</p>
  <p>I've created sample git repo here:
  <a href="https://github.com/rajeshhazari/spring-boot-samples" rel="nofollow noreferrer">https://github.com/rajeshhazari/spring-boot-samples</a></p>
  <p>Any help, suggestion on this will be much appreciated.</p>
  <p>My Entities, Repo and Junit testcase and stacktrace:</p>
  <pre><code>org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.Delete(rootId=1, propertyPath=appUsersAuthList)
  
      at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:89)
      at org.springframework.data.jdbc.core.AggregateChangeExecutor.lambda$execute$0(AggregateChangeExecutor.java:50)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
      at org.springframework.data.relational.core.conversion.DefaultAggregateChange.forEachAction(DefaultAggregateChange.java:116)
      at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:50)
      at org.springframework.data.jdbc.core.JdbcAggregateTemplate.store(JdbcAggregateTemplate.java:339)
      at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:149)
      at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:55)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72)
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382)
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
      at com.sun.proxy.$Proxy69.save(Unknown Source)
      at com.example.test.springjdbcsample1.springjdbcsampletest1Tests.repositoryTest(springjdbcsampletest1Tests.java:54)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
      at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
      at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
      at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
      at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
      at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
      at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
      at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
      at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
      at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
      at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
      at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM &quot;appusers_auth&quot; WHERE &quot;appusers_auth&quot;.&quot;userid&quot; = ?]; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table &quot;appusers_auth&quot; not found; SQL statement:
  DELETE FROM &quot;appusers_auth&quot; WHERE &quot;appusers_auth&quot;.&quot;userid&quot; = ? [42102-200]
      at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
      at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1443)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
      at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
      at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:883)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:321)
      at org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy.delete(DefaultDataAccessStrategy.java:222)
      at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeDelete(JdbcAggregateChangeExecutionContext.java:144)
      at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:74)
      ... 92 more
  Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table &quot;appusers_auth&quot; not found; SQL statement:
  DELETE FROM &quot;appusers_auth&quot; WHERE &quot;appusers_auth&quot;.&quot;userid&quot; = ? [42102-200]
      at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
      at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
      at org.h2.message.DbException.get(DbException.java:205)
      at org.h2.message.DbException.get(DbException.java:181)
      at org.h2.command.Parser.readTableOrView(Parser.java:7628)
      at org.h2.command.Parser.readTableOrView(Parser.java:7599)
      at org.h2.command.Parser.readSimpleTableFilter(Parser.java:1350)
      at org.h2.command.Parser.parseDelete(Parser.java:1377)
      at org.h2.command.Parser.parsePrepared(Parser.java:914)
      at org.h2.command.Parser.parse(Parser.java:843)
      at org.h2.command.Parser.parse(Parser.java:815)
      at org.h2.command.Parser.prepareCommand(Parser.java:738)
      at org.h2.engine.Session.prepareLocal(Session.java:657)
      at org.h2.engine.Session.prepareCommand(Session.java:595)
      at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235)
      at org.h2.jdbc.JdbcPreparedStatement.&lt;init&gt;(JdbcPreparedStatement.java:76)
      at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:352)
      at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
      at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
      at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:237)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
      ... 98 more
  
  </code></pre>
  <pre><code>@Table(&quot;APPUSERS&quot;)
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  public class AppUsers implements Serializable {
      private @Id
      Long userid;
      private String username;
      private String password;
      private String email;
  
      @MappedCollection(idColumn=&quot;userid&quot;, keyColumn=&quot;userid&quot;)
      private Set&lt;AppUsersAuth&gt; appUsersAuthList ;
  }
  </code></pre>
  <pre><code>public interface AppUserRepository extends CrudRepository&lt;AppUsers, Long&gt; {
  
      @Query(&quot;select * from APPUSERS where upper(email) = upper(:email)   &quot;)
      AppUsers findByEmail(@Param(&quot;email&quot;) String email);
  }
  </code></pre>
  <pre><code>@Table(&quot;appusers_auth&quot;)
  @Data
  public  class AppUsersAuth {
      @Column(&quot;auth_user_id&quot;)
      private Long authUserId;
      @Column(&quot;userid&quot;)
      private Long userid;
      private String email;
      @Column(&quot;role_id&quot;)
      private String roleId;
      @Column(&quot;username&quot;)
      private String username;
  
  
  }
  </code></pre>
  <pre><code>CREATE TABLE APPUSERS(
    userid identity not null auto_increment PRIMARY KEY ,
    username VARCHAR(100) not null unique ,
    password VARCHAR(100) not null,
    email VARCHAR(100) not null unique
  );
  
  CREATE TABLE appusers_auth (
   auth_user_id identity not null auto_increment PRIMARY KEY  ,
   userid BIGINT NOT NULL ,
   username text,
   email text NOT NULL,
   role_id VARCHAR(50) NOT NULL,
   updated_time timestamp default CURRENT_TIMESTAMP
  );
  </code></pre>
  <pre><code>@ActiveProfiles(&quot;test&quot;)
  @ExtendWith(SpringExtension.class)
  @ContextConfiguration(classes = SpringjdbcsampletestApp.class)
  @TestPropertySource(locations=&quot;classpath:application-test.properties&quot;)
  public class springjdbcsampletest1Tests {
  
      @Autowired
      AppUserRepository appUserRepository;
  
      @Test
      void repositoryTest() {
          //appUserRepository.deleteAll();
          AuthoritiesMaster authoritiesMaster = new AuthoritiesMaster();
          authoritiesMaster.setRoleId(&quot;role_basic&quot;);
          authoritiesMaster.setRoledesc(&quot;Basic Role Desc&quot;);
  
          //appUsersAuth.setRoleId(authoritiesMaster.getRoleId());
          AppUsersAuth appUsersAuth = new AppUsersAuth();
          appUsersAuth.setEmail(&quot;test1@mail.com&quot;);
          appUsersAuth.setRoleId(&quot;role_basic&quot;);
  
          AppUsers appUsers = new AppUsers();
          appUsers.setUsername(&quot;test1&quot;);
          appUsers.setEmail(&quot;test1@mail.com&quot;);
          appUsers.setPassword(&quot;test123&quot;);
  
  
          appUsersAuth.setUsername(appUsers.getUsername());
          appUsersAuth.setUserid(appUsers.getUserid());
          appUsers = appUserRepository.save(appUsers);
          appUsersAuth.setUserid(appUsers.getUserid());
          appUsers = appUserRepository.save(appUsers);
          Assertions.assertThat(appUsers).isNotNull();
          Assertions.assertThat(appUsers.getAppUsersAuthList()).isNotNull();
          Set&lt;AppUsersAuth&gt; appUsersAuthList = appUsers.getAppUsersAuthList();
          for (final AppUsersAuth usersAuth : appUsersAuthList) {
  
              /*Assertions.assertThat(usersAuth.getAppAuthoritiesMasterList()).isNotNull();
              List&lt;AuthoritiesMaster&gt; authoritiesMasterList = usersAuth.getAppAuthoritiesMasterList();
              authoritiesMasterList.forEach(appUsersAuthMaster -&gt; {
                  Assertions.assertThat(appUsersAuthMaster).isNotNull();
                  Assertions.assertThat(appUsersAuthMaster.getRoleId()).isEqualToIgnoringCase(&quot;role_basic&quot;);
              });*/
          }
          Assertions.assertThat(appUsers.getEmail()).isNotNull();
      }
  
  }
  
  </code></pre>