验证架构编程中使用Hibernate

问题描述 投票:1回答:2

在摩西项目与模式验证运行java app的办法就是通过这种结构(当使用弹簧):

spring.jpa.hibernate.ddl-auto=validate

我遇到了,我需要在运行过程中在特定的时间来验证我的模式出了问题,有没有什么办法来实现呢?

我看到休眠与AbstractSchemaValidator管理它,我使用的弹簧,休眠,我没有找到任何信息,如何处理它, 我发现的唯一的事情是How to validate database schema programmatically in hibernate with annotations? ,但它是在旧版本spring-boot的去除

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

有任何想法吗?

java spring hibernate jpa
2个回答
2
投票

这是解决办法,如果你使用的情况下,需要:

  • 粒状和明确的控制哪些模式的一部分应该被验证
  • 需要的是验证多个模式
  • 需要的是验证未使用的服务模式,在其计划的验证运行
  • 通过应用程序使用的数据库连接不应该通过验证以任何方式影响(意思是,你不希望借用主连接池连接)

如果上述适用于您的需求,超过此为示范如何做计划的模式验证:

  1. 来源
@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties(ScheamValidatorProperties.class)
public class SchemaValidatorApplication {
     public static void main(String[] args) {
       SpringApplication.run(SchemaValidatorApplication.class, args);
    }
}

@ConfigurationProperties("schema-validator")
class ScheamValidatorProperties {
    public Map<String, String> settings = new HashMap<>();

    public ScheamValidatorProperties() {
    }

    public Map<String, String> getSettings() { 
        return this.settings;
    }

    public void setSome(Map<String, String> settings) { 
        this.settings = settings;
    }
}

@Component
class ScheduledSchemaValidator {

    private ScheamValidatorProperties props;

    public ScheduledSchemaValidator(ScheamValidatorProperties props) {
        this.props = props;
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void validateSchema() {
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(props.getSettings())
            .build();

        Metadata metadata = new MetadataSources(serviceRegistry)
            .addAnnotatedClass(Entity1.class)
            .addAnnotatedClass(Entity2.class)
            .buildMetadata();

        try {
            new SchemaValidator().validate(metadata, serviceRegistry);
        } catch (Exception e) {
            System.out.println("Validation failed: " + e.getMessage());
        } finally {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
}

@Entity
@Table(name = "table1")
class Entity1 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    Entity1() {}

    public Long getId() {
        return id;
    }

}

@Entity
@Table(name = "table2")
class Entity2 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    Entity2() {}

    public Long getId() {
        return id;
    }
}
  1. schema.sql文件
CREATE DATABASE IF NOT EXISTS testdb;

CREATE TABLE IF NOT EXISTS `table1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `table2` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);

  1. application.yml
spring:
  cache:
    type: none
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3309/testdb?useSSL=false&nullNamePatternMatchesAll=true&serverTimezone=UTC&allowPublicKeyRetrieval=true
    username: test_user
    password: test_password
    testWhileIdle: true
    validationQuery: SELECT 1
  jpa:
    show-sql: false
    database-platform: org.hibernate.dialect.MySQL8Dialect
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQL8Dialect
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: false
      hibernate.hbm2ddl.auto: validate

schema-validator:
    settings:
        connection.driver_class: com.mysql.cj.jdbc.Driver
        hibernate.dialect: org.hibernate.dialect.MySQL8Dialect
        hibernate.connection.url: jdbc:mysql://localhost:3309/testdb?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
        hibernate.connection.username: test_user
        hibernate.connection.password: test_password
        hibernate.default_schema: testdb

  1. 泊坞窗,compose.yml
version: '3.0'

services:
  db:
    image: mysql:8.0.14
    restart: always
    ports:
     - 3309:3306
    environment:
      MYSQL_ROOT_PASSWORD: test_password
      MYSQL_DATABASE: testdb
      MYSQL_USER: test_user
      MYSQL_PASSWORD: test_password

2
投票

如果你想让SchemaValidator重用连接配置,并且在项目中已经配置,而随后再次将它们定义为模式验证的映射信息,你应该考虑我的解决方案,使得你是干性,不需要维护这些配置在两个不同的地方。

其实,SchemaValidator要求是Metadata实例,它是自举休眠时可用。但是,我们可以使用Hibernate集成API(如here描述)捕捉到它,使得我们以后可以验证他们。

(1)创建SchemaValidateService它实现休眠集成API来捕获Metadata。还设置一个@Scheduled方法以验证在所希望的时间的模式。

@Component
public class SchemaValidateService implements Integrator {

    private Metadata metadata;

    @Override
    public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {
        this.metadata = metadata;
    }

    @Override
    public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    }

    //Adjust the scheduled time here
    @Scheduled(cron = "0 0/1 * * * ?")
    public void validate() {
        try {
            System.out.println("Start validating schema");
            new SchemaValidator().validate(metadata);
        } catch (Exception e) {
            //log the validation error here.
        }
        System.out.println("Finish validating schema....");
    }
}

(2)寄存器SchemaValidateService休眠

@SpringBootApplication
@EnableScheduling
public class App {

    @Bean
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(SchemaValidateService schemaValidateService) {
        return (prop -> {
            List<Integrator> integrators = new ArrayList<>();
            integrators.add(schemaValidateService);
            prop.put("hibernate.integrator_provider", (IntegratorProvider) () -> integrators);
        });
    }
}

此外,该解决方案应该有更好的表现,因为它并不需要为每一个,因为它可以随意在现有的连接池的连接时验证模式的新的数据库连接。

© www.soinside.com 2019 - 2024. All rights reserved.