java org.h2.jdbc.JdbcSQLSyntaxErrorException:未找到架构“WORKING”; SQL语句

suzh9iv8  于 2023-08-01  发布在  Java
关注(0)|答案(1)|浏览(142)

我试图测试我的应用程序使用H2在内存数据库中第一次在我的生活,我面临着一些问题。当我尝试开始测试时,它显示了这样的错误:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "WORKING" not found; SQL statement

字符串
我试着在网上寻找答案,发现代替这段录音的是:

jdbc:h2:file:jdbc:h2:mem:db;SCHEMA=working;DB_CLOSE_DELAY=-1


我可以用这个:

spring.flyway.url=jdbc:h2:mem:db;INIT=CREATE SCHEMA IF NOT EXISTS working;DB_CLOSE_DELAY=-1


但它没有修复这个错误。
以下是我的application.properties配置用于测试:

#Hibernate
spring.datasource.url=${spring.flyway.url}
spring.datasource.username=${spring.flyway.user}
spring.datasource.password=${spring.flyway.password}
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.driver-class-name=org.h2.Driver
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=debug
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.devtools.restart.enabled = false

#flyway
spring.flyway.url=jdbc:h2:mem:db;SCHEMA=working;DB_CLOSE_DELAY=-1
spring.flyway.user=sa
spring.flyway.password=sa
#spring.flyway.schemas=working
spring.flyway.enabled=true


这是我的测试:

UserServiceTest.java

@DataJpaTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserServiceTest {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRepo userRepo;

    @AfterEach
    public void deleteDb() {
        List<UserEntity> users = userRepo.findAll();
        userRepo.deleteAll(users);
    }

    @Test
    @Order(1)
    public void testUserAdding() {
        List<UserAddModel> addModels = getUserAddModels();
        addModels.forEach(m -> isUserGetModelValid(m, userService.addUser(m)));
        addModels = getInvalidUserAddModels();
        addModels.forEach(m -> isUserGetModelValid(m, userService.addUser(m)));
    }

    private List<UserAddModel> getUserAddModels() {
        return new ArrayList<>(List.of(
                new UserAddModel(
                        "Alla",
                        "Pugacheva",
                        null,
                        "+19772395747",
                        null,
                        "AbobaAbobaAboba"
                ),
                new UserAddModel(
                        "Andrey",
                        "Magnolin",
                        "FiftithEnumed",
                        "+19164167722",
                        null,
                        "Fi123sdamJfw+"
                ),
                new UserAddModel(
                        "Andy",
                        "Balk",
                        "Isuch",
                        "+112486759123",
                        "IBiB@Basas",
                        "IBiB@Basas"
                )
        ));

    }

    private void isUserGetModelValid(UserAddModel userAddModel, UserGetModel userGetModel) {
        Assertions.assertAll(
                () -> Assertions.assertNotNull(userGetModel.getId()),
                () -> Assertions.assertEquals(userAddModel.getFirstName(), userGetModel.getFirstName()),
                () -> Assertions.assertEquals(userAddModel.getLastName(), userGetModel.getLastName()),
                () -> Assertions.assertEquals(userAddModel.getPatronymic(), userGetModel.getPatronymic()),
                () -> Assertions.assertEquals(userAddModel.getEmail(), userGetModel.getEmail()),
                () -> Assertions.assertEquals(userAddModel.getPhoneNumber(), userGetModel.getPhoneNumber())
        );
    }

    private List<UserAddModel> getInvalidUserAddModels() {
        return new ArrayList<>(List.of(
                new UserAddModel(
                  "FFRoasd*!TИffВ]\\\"!@}#{$yfdf4?/\\\\f\\\\%^}12н4#)([",
                  "Zsda_q*!fjIJF]\\\"!@}#{$yfdf4?/\\\\f\\\\%^}12н4#)([",
                  "ZHansda-*!ffjja@]\\\"!@}#{$yfdf4?/\\\\f\\\\%^}12н4#)([",
                        "ff@.google.com",
                        "HAHAH +15026812036749(643)40-2-75 go0odLuck",
                        "Э?"
                ),
                new UserAddModel(
                        "Alla",
                        "Pugacheva",
                        null,
                        "sb@.google.com",
                        null,
                        "AbobaAbobaAboba"
                ),
                new UserAddModel(
                        "Andrei",
                        "Balk",
                        "Iv",
                        "+112486759123",
                        "outlook.com",
                        "IlkbB@Ilkbib"
                ),
                new UserAddModel(
                        "Andy",
                        "Balcony",
                        "IVconf",
                        "+112486759123",
                        "mmm@google.com",
                        "AAA?"
                )
            )
        );
    }

    @Nested
    private class UserNestedTests {

    }
}


这是我的迁移sql文件

CREATE TABLE IF NOT EXISTS users (
    id BIGINT NOT NULL,
    first_name varchar(512) NOT NULL,
    last_name varchar(512) NOT NULL,
    patronymic varchar(512),
    phone_number varchar(20) NOT NULL UNIQUE,
    email varchar(64) UNIQUE,
    password varchar(64) NOT NULL,
    state varchar(64) NOT NULL,
    primary key(id)
);


下面是我的UserEntity.java

@Entity
@Table(name = "users", schema = "working")
@Getter @Setter
public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    private String firstName;
    private String lastName;
    private String patronymic;
    @Column(unique = true)
    private String phoneNumber;
    @Column(unique = true)
    private String email;
    private String password;
    private String state;

    public UserEntity() {
    }

    public UserEntity(String firstName, String lastName, String patronymic, String phoneNumber, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.patronymic = patronymic;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    public static UserEntity toModel(UserAddModel userAddModel) {
        UserEntity user = new UserEntity();
        ModelMapper mapper = new ModelMapper();
        mapper.map(userAddModel, user);
        user.state = StateConstantsConfig.STUDENT;
        return user;
    }
}


那么问题出在哪里呢?如果你知道,请告诉我。我真的很感激!

vltsax25

vltsax251#

根据文档,对于SCHEMA的设置,您需要执行命令SET SCHEMA <name>。在您的示例中,它看起来像这样:

  • jdbc:h2:mem:db;SET SCHEMA working;DB_CLOSE_DELAY=-1

但是H2对模式working一无所知。它只知道默认模式PUBLIC。为了帮助它,您需要按照您尝试的方式创建这个模式。完整的URL看起来像这样:

  • jdbc:h2:mem:db;INIT=create schema if not exists working\;SET SCHEMA working;DB_CLOSE_DELAY=-1

请注意,反斜杠\是必需的。
但是如果您不需要任何模式,您将无法使用它。你可以这样命名数据库working

  • spring.flyway.url=jdbc:h2:mem:working;DB_CLOSE_DELAY=-1

相关问题