Answer the question
In order to leave comments, you need to log in
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.
Registered entity classes.
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();
}
}
Answer the question
In order to leave comments, you need to log in
Create a class marked with , in it. method
main
that calls SpringApplication.run
.
I advise you to read the guides to the spring https://spring.io/guides/gs/spring-boot/
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";
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
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 questionAsk a Question
731 491 924 answers to any question