Browse Source

conectando a H2

master
Juanjo 1 year ago
parent
commit
91da37c7ff
4 changed files with 90 additions and 3 deletions
  1. +13
    -0
      src/main/java/es/fp/edu/conecta2/BDConfig.java
  2. +73
    -0
      src/main/java/es/fp/edu/conecta2/BDTempConfig.java
  3. +1
    -1
      src/main/java/es/fp/edu/conecta2/repo/admin/IAdminRepo.java
  4. +3
    -2
      src/main/resources/application.properties

+ 13
- 0
src/main/java/es/fp/edu/conecta2/BDConfig.java View File

@ -6,8 +6,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@ -54,4 +56,15 @@ public class BDConfig {
return em;
}
@Bean(name = "userTransactionManager")
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
}

+ 73
- 0
src/main/java/es/fp/edu/conecta2/BDTempConfig.java View File

@ -0,0 +1,73 @@
package es.fp.edu.conecta2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "adminEntityManagerFactory",
transactionManagerRef = "adminTransactionManger",
basePackages = {"es.fp.edu.conecta2.repo.admin"})
public class BDTempConfig {
@Autowired
private Environment env;
@Bean(name="adminDataSource")
public DataSource userDatasource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(env.getProperty("temporal.datasource.url"));
dataSource.setUsername(env.getProperty("temporal.datasource.username"));
dataSource.setPassword(env.getProperty("temporal.datasource.password"));
dataSource.setDriverClassName(env.getProperty("temporal.datasource.driver-class-name"));
return dataSource;
}
@Bean(name = "adminEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// TODO: 27/01/2023
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(userDatasource());
em.setPackagesToScan("es.fp.edu.conecta2.modelo.admin");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
Map<String,Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("temporal.jpa.hibernate.ddl-auto"));
properties.put("hibernate.show-sql", env.getProperty("temporal.jpa.show-sql"));
properties.put("hibernate.dialect", env.getProperty("temporal.jpa.properties.hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean(name = "adminTransactionManager")
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
}

+ 1
- 1
src/main/java/es/fp/edu/conecta2/repo/admin/IAdminRepo.java View File

@ -3,6 +3,6 @@ package es.fp.edu.conecta2.repo.admin;
import es.fp.edu.conecta2.modelo.admin.Admin;
import org.springframework.data.jpa.repository.JpaRepository;
public interface IAdminRepo extends JpaRepository <Admin, Integer> {
public interface IAdminRepo extends JpaRepository<Admin, Integer> {
}

+ 3
- 2
src/main/resources/application.properties View File

@ -23,9 +23,10 @@ persistente.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
#persistente.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
#persistente.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
temporal.datasource.url=jdbc:h2:file:/home/public/h2
temporal.datasource.url=jdbc:h2:mem:temporal
temporal.datasource.username=sa
temporal.datasource.password=
temporal.datasource.driver-class-name=org.h2.Driver
temporal.jpa.show-sql=true
temporal.jpa.hibernate.ddl-auto=create-drop
temporal.jpa.hibernate.ddl-auto=create-drop
temporal.jpa.database-platform=org.hibernate.dialect.H2Dialect

Loading…
Cancel
Save