Home > Mobile >  How to set configuration using application.properties in Hibernate?
How to set configuration using application.properties in Hibernate?

Time:03-30

I am new to Spring boot development.

What I did?

I tested the sample MySQL integration with Spring boot and tested it works fine.

What I am trying?

I am trying to use Hibernate APIs with my existing project. So I create a util class for creating Hibernate sessionFactory.

My code:

public static void initSessionFactory() {
    System.out.print("initSessionFactory");
    if (sessionFactory == null) {
        try {
            Configuration configuration = new Configuration();

             //***** I don't want configuration here. ******
            // Hibernate settings equivalent to hibernate.cfg.xml's properties
            //Properties settings = new Properties();
            // settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
            // settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_db?useSSL=false");
            // settings.put(Environment.USER, "root");
            // settings.put(Environment.PASS, "root");
            // settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
            // settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            // settings.put(Environment.HBM2DDL_AUTO, "create-drop");
            // settings.put(Environment.SHOW_SQL, "true");
            //  configuration.setProperties(settings);

            //configuration.addAnnotatedClass(Student.class);

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            session = sessionFactory.openSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

My application.properties:(It works fine for without Hibernate)

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/tcc
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true

My problem:

I already configured the jdbc environment & other configuration in my application.properties. It works fine. So I don't want to repeat the same configuration in my Java code. So I commented this configuration.

But without Java configuration it throws "The application must supply JDBC connections" error.

My Question:

How to set configuration from application.properties for Hibernate?

CodePudding user response:

When you set the properties below in your app.properties, the Spring Boot will already make your DB connection ready when the application start.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver

You do not need to create SessionFactory , ServiceRegistery etc. even if you do not want to manage DB connection manually.

This link also explain the steps in more detail. You will see that there is no any custom bean, factory or registerer to establish DB connection. Please see it. https://spring.io/guides/gs/accessing-data-mysql/

  • Related