Spring JPA DDL file generation - how to delete or clean file before generating - TagMerge
5Spring JPA DDL file generation - how to delete or clean file before generatingSpring JPA DDL file generation - how to delete or clean file before generating

Spring JPA DDL file generation - how to delete or clean file before generating

Asked 9 months ago
5 answers

I was faced with the same problem, and found the following way to clean up before schema generation: use BeanFactoryPostProcessor. BeanFactoryPostProcessor is called when all bean definitions are loaded, but no beans are instantiated yet.

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

    public static BeanFactoryPostProcessor schemaFilesCleanupPostProcessor() {
        return bf -> {
            try {
            } catch (IOException e) {
                throw new IllegalStateException(e);

Source: link


Short example using Gradle and Springboot. Assuming you have a project property defining your environment, and "dev" is the one creating DDL files for Postgres.

Excerpt from application.yml:

    database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: POSTGRESQL
      javax.persistence.schema-generation.database.action: drop-and-create
      javax.persistence.schema-generation.scripts.action: drop-and-create
      javax.persistence.schema-generation.scripts.create-target: ./sql/create-postgres.sql
      javax.persistence.schema-generation.scripts.create-source: metadata
      javax.persistence.schema-generation.scripts.drop-target: ./sql/drop-postgres.sql
      javax.persistence.schema-generation.scripts.drop-source: metadata

Add some code in bootRun task to delete the files:

bootRun {
def defaultProfile = 'devtest'
def profile = project.hasProperty("env") ? project.getProperty("env") : defaultProfile
if (profile == 'dev') {delete 'sql/create-postgres.sql'; delete 'sql/drop-postgres.sql';}


Source: link


I only ever use the schema file for testing and solved the problem by specifying the location of the output file as follows:


This works well for Maven but the target directory could be modified for a Gradle build.

Source: link


In this tutorial, aside from the usual Spring Boot dependencies, we’re using JUnit Jupiter as our testing framework and H2 as an in-memory database.
dependencies {
The first question to answer to ourselves is what we need to test. Let’s consider a Spring Data repository responsible for UserEntity objects:
interface UserRepository extends CrudRepository<UserEntity, Long> {
  // query methods
The first option is to create an inferred query:
UserEntity findByName(String name);
What’s nice about this feature is that Spring Data also automatically checks if the query is valid at startup. If we renamed the method to findByFoo() and the UserEntity does not have a property foo, Spring Data will point that out to us with an exception:
  No property foo found for type UserEntity!
If queries become more complex, it makes sense to provide a custom JPQL query:
@Query("select u from UserEntity u where u.name = :name")
UserEntity findByNameCustomQuery(@Param("name") String name);

Source: link


Generate: Maven Project
Java Version: 1.8 (Default)
Spring Boot:2.1.1
Group: com.loiane
Artifact: spring-cloud-mysql
Dependencies: Web, JPA, Lombok, MySQL
public class Contact {

  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
  private String email;
  private String phone;
public interface ContactRepository 
    extends JpaRepository<Contact, Long> { }
public class ContactController {

  private ContactRepository repository;

  ContactController(ContactRepository contactRepository) {
      this.repository = contactRepository;

  // CRUD methods here
public List findAll(){
  return repository.findAll();

Source: link

Recent Questions on jpa

    Programming Languages