BAEL-6326: Spring Boot H2 JdbcSQLSyntaxErrorException expected identifier (#13843)

This commit is contained in:
Azhwani 2023-05-04 08:09:02 +02:00 committed by GitHub
parent d313b76fc7
commit 8d0158aa5b
6 changed files with 103 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<User, Integer> {
}

View File

@ -0,0 +1 @@
spring.sql.init.data-locations=user-data.sql

View File

@ -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');

View File

@ -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<User> users = userRepository.findAll();
assertThat(users).hasSize(2);
}
}