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
3
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.
 */
@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);
            }
        };
    }

Source: link

0

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:

spring:
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: POSTGRESQL
    properties:
      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

0

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

spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=target/test-classes/schema.sql

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

Source: link

0

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 {
  compile('org.springframework.boot:spring-boot-starter-data-jpa')
  compile('org.springframework.boot:spring-boot-starter-web')
  runtime('com.h2database:h2')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile('org.junit.jupiter:junit-jupiter-engine:5.2.0')
}
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:
org.springframework.data.mapping.PropertyReferenceException: 
  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

0

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
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Contact {

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

  private String name;
  private String email;
  private String phone;
}
@Repository
public interface ContactRepository 
    extends JpaRepository<Contact, Long> { }
@RestController
@RequestMapping({"/contacts"})
public class ContactController {

  private ContactRepository repository;

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

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

Source: link

Recent Questions on jpa

    Programming Languages