I
I
Igor2021-03-18 12:57:52
Spring
Igor, 2021-03-18 12:57:52

How to set up a project so that it raises the schema in the database using hibernate?

I'm trying to create my first application "Library".
In the final (intermediate) result, I want to get an application that can perform crud operations via rest-api.

Using MySQL, I created 3 tables - Books, Clients, Orders. Connected the database to Intellij Idea.
60531f425bb5e503582339.png

Registered entity classes.
605320604493d022074127.png

As far as I understand, then I need to check if the project raises the database, and for this I need to register the config.
I also tried to prescribe it, but I'm not sure if I did it right.

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@ComponentScan("service")
@EnableJpaRepositories(basePackages = "repository")
public class Config {

    private static final String URL = "jdbc:mysql://localhost:3306/library_project?serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(URL, USER, PASSWORD);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        return properties;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean managerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        managerFactoryBean.setDataSource(dataSource());
        managerFactoryBean.setPackagesToScan("model");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        managerFactoryBean.setJpaVendorAdapter(vendorAdapter);
        managerFactoryBean.setJpaProperties(additionalProperties());

        return managerFactoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);

        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}


It also remains a mystery how to run the project in order to check the performance of at least something, because Main with psvm should not be here, if I understand everything correctly. Tell me, please, what steps should I take to further write the application and where should the entry point to the application be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cheypnow, 2021-03-18
@Cheypnow

Create a class marked @SpringBootApplicationwith , in it. method mainthat calls SpringApplication.run.
I advise you to read the guides to the spring https://spring.io/guides/gs/spring-boot/

O
Orkhan Hasanli, 2021-03-18
@azerphoenix

Good afternoon.
Placing DB accesses inside Java is not recommended.

private static final String URL = "jdbc:mysql://localhost:3306/library_project?serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

It is better to put it in properties or yml
Here is an example:
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

https://spring.io/guides/gs/accessing-data-mysql/
https://www.baeldung.com/java-connect-mysql

It also remains a mystery how to run the project in order to check the performance of at least something, because Main with psvm should not be here, if I understand everything correctly.

@SpringBootApplication
public class Application {

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

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question