1

I created a SpringBoot 1.5.11.RELEASE and Hibernate 5 project. Everything was working fine.

However, when I changed SpringBoot version to 2.0.0.RELEASE the spring.jpa.hibernate.ddl-auto=create does not work. I have tried this stackoverflow post, but no luck.

Here is the stack trace of SpringBoot 1.5.11.RELEASE :

2018-04-06 16:27:52.512 INFO 6792 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 2018-04-06 16:27:52.526 INFO 6792 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2018-04-06 16:27:52.587 INFO 6792 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.0.12.Final} 2018-04-06 16:27:52.589 INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-04-06 16:27:52.591 INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000021: Bytecode provider name : javassist 2018-04-06 16:27:52.735 INFO 6792 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-04-06 16:27:52.833 INFO 6792 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:53.180 INFO 6792 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export Hibernate: drop table if exists person Hibernate: create table person (id integer not null auto_increment, name varchar(255), primary key (id)) 2018-04-06 16:27:53.619 INFO 6792 --- [
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete 2018-04-06 16:27:53.646 INFO 6792 --- [
main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' SessionFactory Created 2018-04-06 16:27:54.528 WARN 6792 --- [ main] org.hibernate.orm.connections : HHH10001002: Using Hibernate built-in connection pool (not for production use!) 2018-04-06 16:27:54.529 INFO 6792 --- [ main] org.hibernate.orm.connections : HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel] 2018-04-06 16:27:54.529 INFO 6792 --- [ main] org.hibernate.orm.connections : HHH10001001: Connection properties: {user=root, password=****} 2018-04-06 16:27:54.529 INFO 6792 --- [ main] org.hibernate.orm.connections : HHH10001003: Autocommit mode: false 2018-04-06 16:27:54.532 INFO 6792 --- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1) 2018-04-06 16:27:54.539 INFO 6792 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:54.855 INFO 6792 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup Hibernate: insert into person (name) values (?) Saved!! 2018-04-06 16:27:54.954 INFO 6792 --- [ main] com.hib.Hibernate5Application : Started Hibernate5Application in 4.051 seconds (JVM running for 4.802)


Here is the stack trace of SpringBoot 2.0.0.RELEASE :

SessionFactory Created 2018-04-06 16:24:17.011 INFO 5092 --- [
main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.14.Final} 2018-04-06 16:24:17.013 INFO 5092 --- [
main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2018-04-06 16:24:17.915 INFO 5092 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-04-06 16:24:17.989 WARN 5092 --- [ main] org.hibernate.orm.connections.pooling : HHH10001002: Using Hibernate built-in connection pool (not for production use!) 2018-04-06 16:24:17.994 INFO 5092 --- [ main] org.hibernate.orm.connections.pooling : HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel] 2018-04-06 16:24:17.994 INFO 5092 --- [ main] org.hibernate.orm.connections.pooling : HHH10001001: Connection properties: {user=root, password=****} 2018-04-06 16:24:17.994 INFO 5092 --- [ main] org.hibernate.orm.connections.pooling : HHH10001003: Autocommit mode: false 2018-04-06 16:24:17.996 INFO 5092 --- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1)

2018-04-06 16:24:18.230 INFO 5092 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:24:19.041 INFO 5092 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2018-04-06 16:24:19.058 INFO 5092 --- [
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.

2018-04-06 16:24:19.419 INFO 5092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-04-06 16:24:19.420 INFO 5092 --- [
main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure 2018-04-06 16:24:19.427 INFO 5092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-04-06 16:24:19.441 INFO 5092 --- [ main] com.hib.Hibernate5Application : Started Hibernate5Application in 3.742 seconds (JVM running for 4.541) Hibernate: insert into person (name) values (?) Saved!!


Hibernate 5 configuration :

@Configuration
public class HinernateConfig {

    private StandardServiceRegistry standardServiceRegistry;
    private SessionFactory sessionFactory;

    @Bean
    public SessionFactory sessionFactory() {
        try {
            standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
            Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().build();
            sessionFactory =  metadata.getSessionFactoryBuilder().build();
            return sessionFactory;
        } catch (Exception e) {
            System.out.println("Exception occured while creating SessionFactory");
            e.printStackTrace();
            if (standardServiceRegistry != null) {
                StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
            }
        }
        return sessionFactory;
    }

    @PostConstruct
    public void post() {
        System.out.println("SessionFactory Created");
    }

}

Main application :

@SpringBootApplication
public class Application implements CommandLineRunner {

    private SessionFactory sessionFactory;

    public Application(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Person person = new Person("Mehraj Malik");
        session.save(person);
        System.out.println("Saved!!");
        session.getTransaction().commit();
        session.close();
    }
}

Hibernate Xml config

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hotel</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping class="com.hib.entity.Person"/>
    </session-factory>
</hibernate-configuration>

application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/Hotel
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

In Boot 2.0 stack trace the below part does not appear :

Running hbm2ddl schema export Hibernate: drop table if exists person Hibernate: create table person (id integer not null auto_increment, name varchar(255), primary key (id))

Github : https://github.com/Mehraj123/Hib5Boot2.0

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
  • but your code is able to run anyways as you are able to save the person also with Spring Boot 2.0.0 as your log file says: `Hibernate5Application in 3.742 seconds (JVM running for 4.541) Hibernate: insert into person (name) values (?) Saved!!`. Have you tried to run your application with Spring Boot 2.0.0 with an empty database? – rieckpil Apr 06 '18 at 11:01
  • You aren't using JPA so why should the `spring.jpa` properties apply. You are configuring your own `SessionFactory` without even as much as using the properties defined in the `application.properties`. Also why use plain hibernate, use JPA and use the `EntityManager` instead, saves you from configuring the `SessionFactory` yourself and you can benefit from the autoconfiguration from Spring Boot. – M. Deinum Apr 06 '18 at 11:01
  • @rieckpil yes, it's saving the record to DB, however, it should delete the table and it's data before inserting the new record. – Mehraj Malik Apr 06 '18 at 11:04
  • shouldn't you therefor use `create-drop` for `ddl-auto`? – rieckpil Apr 06 '18 at 11:09
  • @M.Deinum I searched google for hibernate 5 configuration and this the way they have created `SessionFactory` that's why I did in this way. If you have any suggestion or link that would be very helpful. – Mehraj Malik Apr 06 '18 at 11:12
  • @rieckpil nor `create-drop` worked – Mehraj Malik Apr 06 '18 at 11:13
  • Drop the configuration, remove your hibernate configuration and use JPA (with hibernate as the provider). – M. Deinum Apr 06 '18 at 11:13
  • @M.Deinum did you mean `application.properties` and `xml configuration`? – Mehraj Malik Apr 06 '18 at 11:15
  • 1
    I ment remove your hibernate based configuration class and the hibernate xml configuration. You are using Spring Boot that auto-configures JPA for you... You are making it more complex with trying to do manual configuration (and stil expect auto-configuration to work). – M. Deinum Apr 06 '18 at 11:16

1 Answers1

1

As mentioned by M.Deinum I've deleted the following classes and resource file and it works :

  • HinernateConfig
  • hibernate.cfg.xml

And changes in Main application

@SpringBootApplication
public class Hibernate5Application implements CommandLineRunner {

    private EntityManagerFactory managerFactory;

    public Hibernate5Application(EntityManagerFactory managerFactory) {
        this.managerFactory = managerFactory;
    }

    public static void main(String[] args) {
        SpringApplication.run(Hibernate5Application.class, args);
    }

    @Override
    @Transactional
    public void run(String... args) throws Exception {
        EntityManager entityManager = managerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        Person person = new Person("Mehraj Malik");
        entityManager.persist(person);
        System.out.println("Saved!!");
        entityManager.close();
    }
}

I've updated my github repo

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85