2

Spring / HibernateJpaVendorAdapter under JPA 2.1 is not the executing schema generation of Entity model / Metadata info.

I have added two code snippets and the persistence.xml below. (Checkout my JPA 2.1 properties about schema generation... they seem to be okay. (?))

When I re-run my webapplication (restart of Tomcat) the scripts files are not created on my filesystem. (I even did a complete file search on my filesystem whether they were saved on a different place than expected. But no sign of these files on my filesystem). What is wrong with the code / persistence.xml below? I checked with the javadocs of Spring's HibernateJpaVendor class but I didn't see anything which could point me to the cause.

Question: What's wrong here... I have been breaking my head on this one?

Remark 1: However, when I move my JPA 2.1 schema generation properties from HibernateJpaVendor to persistence.xml the files are generated successfully. (These properties have been put in comments in my persistence.xml to reflect the problem I am describing above.)

Remark 2: I am using Hibernate 5.1.0.Final and Spring framework 4.2.5.RELEASE

Versions in POM:

<!-- hibernate - validator JSR303-->
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
<!-- hibernate -->
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
<hibernate.version>5.1.0.Final</hibernate.version>

<!-- spring framework -->
<spring.framework.version>4.2.5.RELEASE</spring.framework.version>    

Properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/ob?autoReconnect=true
jdbc.username=<user>
jdbc.password=<password>
jdbc.db=obi
jpa.database=MYSQL
hbm2ddl=auto
jpa.showSql=true
jpa.generateDdl=false
jpa.persistenceUnit=obPU
hibernate.hbm2ddl.auto=create-update
hibernate.archive.autodetection=class
c3p0.acquire_increment=5
c3p0.idle_test_period=100
c3p0.max_size=100
c3p0.max_statements=0
c3p0.min_size=10

Persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="obPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.company.ob.domain.model.Obi</class>
    <class>com.company.ob.domain.model.Con</class>
    <class>com.company.ob.domain.model.ImageMeta</class>

    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <!--<properties>-->
       <!--<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>-->
       <!--<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>-->
       <!--<property name="javax.persistence.schema-generation.scripts.create-target" value="obituaries-create.sql"/>-->
       <!--<property name="javax.persistence.schema-generation.scripts.drop-target" value="obituaries-drop.sql"/>-->
       <!--<property name="javax.persistence.sql-load-script-source" value="obituaries-insert.sql"/>-->
   <!--</properties>-->
</persistence-unit>

Code snippets from my Spring Java @Configuration file. My cofiguration is something like this example but not identical (to give an idea/picture):

@Bean(name = "entityManagerFactory")
public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaAdapter) {

    LocalContainerEntityManagerFactoryBean fb = new LocalContainerEntityManagerFactoryBean();
    fb.setDataSource(dataSource);
    fb.setJpaVendorAdapter(jpaAdapter);
    fb.setPersistenceUnitName(persUnit);
    fb.setPackagesToScan("com.company.ob.domain.model"); //if set info persistence is not needed. Scan based on Spring and not on JPA
    fb.afterPropertiesSet();
    return fb.getObject();
}

@Bean
public JpaVendorAdapter jpaAdapter() {

    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();

    adapter.setDatabase(Database.valueOf(database));
    adapter.setShowSql(Boolean.valueOf(showSql));

    // not setting to true because not possible as of Hibernate v5 if using JPA 2.1 schema generation (see javadocs Hibernate)
    //adapter.setGenerateDdl(Boolean.valueOf(genDdl)); 

    //adapter.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", hbm2ddl);
    adapter.getJpaPropertyMap().put("javax.persistence.schema-generation.database.action", "drop-and-create");
    adapter.getJpaPropertyMap().put("javax.persistence.schema-generation.scripts.action", "drop-and-create");
    adapter.getJpaPropertyMap().put("javax.persistence.schema-generation.scripts.create-target", "ob-create.sql");

    adapter.getJpaPropertyMap().put("javax.persistence.schema-generation.scripts.drop-target", "ob-drop.sql");
    adapter.getJpaPropertyMap().put("javax.persistence.database-product-name", "MySQL");

//        adapter.getJpaPropertyMap().put("javax.persistence.sql-load-script-source", "ob-insert.sql");

    return adapter;
}
user2120188
  • 427
  • 1
  • 4
  • 16

0 Answers0