From 8d0158aa5bd8501319d954b137f01ec67c5baa5d Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Thu, 4 May 2023 08:09:02 +0200 Subject: [PATCH] BAEL-6326: Spring Boot H2 JdbcSQLSyntaxErrorException expected identifier (#13843) --- .../h2/exceptions/SpringBootH2Exceptions.java | 15 ++++++++ .../baeldung/h2/exceptions/models/User.java | 38 +++++++++++++++++++ .../h2/exceptions/repos/UserRepository.java | 11 ++++++ .../src/main/resources/app-h2.properties | 1 + .../src/main/resources/user-data.sql | 8 ++++ ...SpringBootH2ExceptionsIntegrationTest.java | 30 +++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/SpringBootH2Exceptions.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/repos/UserRepository.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/resources/app-h2.properties create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/resources/user-data.sql create mode 100644 persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2/exceptions/SpringBootH2ExceptionsIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/SpringBootH2Exceptions.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/SpringBootH2Exceptions.java new file mode 100644 index 0000000000..c7684423a2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/SpringBootH2Exceptions.java @@ -0,0 +1,15 @@ +package com.baeldung.h2.exceptions; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@PropertySource("classpath:app-h2.properties") +public class SpringBootH2Exceptions { + + public static void main(String... args) { + SpringApplication.run(SpringBootH2Exceptions.class, args); + } + +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java new file mode 100644 index 0000000000..e54e725fd0 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java @@ -0,0 +1,38 @@ +package com.baeldung.h2.exceptions.models; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User { + + @Id + private int id; + private String login; + private String password; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/repos/UserRepository.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/repos/UserRepository.java new file mode 100644 index 0000000000..d014fb16c6 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/repos/UserRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.h2.exceptions.repos; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.h2.exceptions.models.User; + +@Repository +public interface UserRepository extends JpaRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/app-h2.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/app-h2.properties new file mode 100644 index 0000000000..bb88e7fef4 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/app-h2.properties @@ -0,0 +1 @@ +spring.sql.init.data-locations=user-data.sql diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/user-data.sql b/persistence-modules/spring-boot-persistence-h2/src/main/resources/user-data.sql new file mode 100644 index 0000000000..cea758c74e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/user-data.sql @@ -0,0 +1,8 @@ +/* These commented lines will cause Spring Boot to fail at startup + * + * INSERT INTO user VALUES (1, 'admin', 'p@ssw@rd'); + * INSERT INTO user VALUES (2, 'user', 'userpasswd'); + * +*/ +INSERT INTO "user" VALUES (1, 'admin', 'p@ssw@rd'); +INSERT INTO "user" VALUES (2, 'user', 'userpasswd'); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2/exceptions/SpringBootH2ExceptionsIntegrationTest.java b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2/exceptions/SpringBootH2ExceptionsIntegrationTest.java new file mode 100644 index 0000000000..94cf08fb76 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2/exceptions/SpringBootH2ExceptionsIntegrationTest.java @@ -0,0 +1,30 @@ +package com.baeldung.h2.exceptions; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.h2.exceptions.models.User; +import com.baeldung.h2.exceptions.repos.UserRepository; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringBootH2Exceptions.class) +public class SpringBootH2ExceptionsIntegrationTest { + + @Autowired + private UserRepository userRepository; + + @Test + public void givenValidInitData_whenCallingFindAll_thenReturnData() { + List users = userRepository.findAll(); + + assertThat(users).hasSize(2); + } + +}