Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA DDL文件生成-如何在生成之前删除或清理文件_Jpa_Spring Data Jpa_Ddl - Fatal编程技术网

Spring JPA DDL文件生成-如何在生成之前删除或清理文件

Spring JPA DDL文件生成-如何在生成之前删除或清理文件,jpa,spring-data-jpa,ddl,Jpa,Spring Data Jpa,Ddl,我正在使用此设置生成ddl文件: spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create spring.jpa.properties.javax.persistence.schema-generation.scripts.create

我正在使用此设置生成ddl文件:

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=./ddl/schema.sql
通过Maven验证阶段的特定测试执行生成:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@TestPropertySource(locations = "/ddl.properties")
public class GenerateDDL {

    @Autowired
    private EntityManager em;

    @Test
    public void generateDDL() throws IOException {
        em.close();
        em.getEntityManagerFactory().close();
    }

}
这工作得很好,但有一个问题:生成器不创建新文件,而是总是附加它的内容

是否有一种方法或设置可以让generator始终创建新文件或清理旧文件

在测试中删除它将在生成后删除它。我们还需要在git上发布该文件,因此它不会在
target
中生成

更新 Hibernate中似乎至少没有解决方案(直到Hibernate 6):


在创建持久性上下文之前,有没有办法钩住Spring上下文的创建?在那里,我可以删除该文件。

我也遇到了同样的问题,并找到了以下在生成架构之前进行清理的方法:使用BeanFactoryPostProcessor。当加载了所有bean定义,但尚未实例化任何bean时,将调用BeanFactoryPostProcessor

 /**
 * Remove auto-generated schema files before JPA generates them.
 * Otherwise new content will be appended to the files.
 */
@Configuration
public class SchemaSupport {

    @Bean
    public static BeanFactoryPostProcessor schemaFilesCleanupPostProcessor() {
        return bf -> {
            try {
                Files.deleteIfExists(Path.of("schema-drop-auto.sql"));
                Files.deleteIfExists(Path.of("schema-create-auto.sql"));
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        };
    }

在启动时使用maven删除文件?这将是一个错误的选项-如果从maven或IDE运行测试,则应该有相同的输出。但似乎没有其他选择?!