djarnis73 / jpa-table-backed-enum

Sample spring boot application that demonstrates how to map a java enum as a JPA entity using hibernate.
2 stars 0 forks source link

spring.datasource.data #1

Open frolovgo opened 3 years ago

frolovgo commented 3 years ago

Hello @djarnis73, is it possible to use this solution with data-initialisation from *.sql-files(to populate related tables)? For example, I have added "spring.datasource.data=classpath:data.sql" to application.properties and add data.sql with this string: insert into product(new_style_color_id) values(1); --Referential integrity constraint violation during application startup

I have added something like this to InitializeEnumBackedTables.initializeEnumTables (and moved problem SQL to additionalData.sql)

        entityManager.flush();
        new ResourceDatabasePopulator(false, false, "UTF-8", new ClassPathResource("additionalData.sql")).execute(dataSource);

to work around the issue. But maybe there is more elegant solution ?

djarnis73 commented 3 years ago

Hi @frolovgo

I did not experiment with that myself, but I do recall that some of the tricky stuff with this was getting the InitializeEnumBackedTables hooked into spring at the right point of bootstrapping.

I think that the later versions of spring boot have reworked this logic a bit, try digging into https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization.

Best regards Jens