0

I'm suing Spring Boot with JPA. I tried to create src/main/resources/import.sql script for provisioning users into application properties:

spring.datasource.jndi-name=java:/global/production
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.datasource.data=classpath:import.sql

But unfortunately when I deploy the application the script is not run. Do you know with JPA what is the proper way to create SQL provisioning script?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • The JPA spec has a persistence property `javax.persistence.sql-load-script-source` which allows you to populate your database with initial data. Any compliant JPA provider would support that. But then that would be in the JPA spec for all to see –  Jul 31 '18 at 12:22
  • Thanks. Do you know is it going to be executed after creating JPA entities? – Peter Penzov Jul 31 '18 at 12:53
  • It is executed after creating the EMF, as per the spec –  Jul 31 '18 at 12:53
  • is there a solution which works with JPA and Hibernate? – Peter Penzov Jul 31 '18 at 12:57
  • I've told you the JPA "solution" of how to load up data when creating an EMF. If your JPA provider (Hibernate) is compliant then it will support that property. If it isn't compliant with that property (unlikely?) then a). you should raise a bug on it, and b). you should ask yourself the question why you chose it –  Jul 31 '18 at 14:12
  • I tried this: I placed import.sql under src/main/resources and I configured javax.persistence.sql-load-script-source=import.sql but nothing happens. Any ideas? – Peter Penzov Jul 31 '18 at 16:56

2 Answers2

0

If you need to initialized your database (if I understand you correctly), all you need to do in your Spring Boot project is just provide file data.sql with your DML scripts in your root classpath location (i.e. in resources dir):

Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.

Source.

Another approach you can find here.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • only sql file with name `data` will be executed? I have configured Spring to create db schema based on my entities. – Peter Penzov Jul 31 '18 at 10:34
  • You can use `import.sql` file as well - in this case [Hibernate will populate your database](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-hibernate). But you should set `ddl-auto` property to `create` or `create-drop` – Cepr0 Jul 31 '18 at 10:40
  • Any other proposals? – Peter Penzov Jul 31 '18 at 10:41
0

With the @Sql annotation you can specify the path of the file you want to run before the tests. For example:

@TestPropertySource("classpath:application-test.properties")
@Sql("/data.sql")
class MyClassTest

You can read more in here.

Jean Patricio
  • 331
  • 3
  • 8