There are multiple ways in OpenHub how to store custom business data.
See following variants:
Configure EntityManager, to scan custom packages, with OpenHub configuration property ohf.jpa.additional-packages
Example:
ohf.jpa.additional-packages=org.openhubframework.openhub.ri.persistence |
Create custom dao, or configure spring-data-jpa repositories.
Configuration of spring-data-jpa repositories can look like this:
@EnableJpaRepositories( entityManagerFactoryRef = "customEntityManagerFactory", basePackages = "org.openhubframework.openhub.ri.persistence.repository") @Configuration public class SecondDataSourceConfiguration { @Bean @Qualifier("customEntityManagerFactory") LocalContainerEntityManagerFactoryBean customEntityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(false); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); factoryBean.setPackagesToScan(User.class.getPackage().getName()); factoryBean.setJtaDataSource(customDataSource()); return factoryBean; } @Bean @Qualifier("customDataSource") DataSource customDataSource() { return DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/testdb?useSSL=false&nullNamePatternMatchesAll=true") .driverClassName("com.mysql.jdbc.Driver") .username("*****") .password("*****") .build(); } |
Configure OpenHub to register JpaTransactionManager for OpenHub entities. By default, it is disabled, and transactionManager is configured by SpringBoot.
ohf.jpa.transaction-manager.enabled=true |
Configuration of spring-data-jpa repositories can look like this :
@EnableJpaRepositories( entityManagerFactoryRef = "customEntityManagerFactory", transactionManagerRef = "customTransactionManager", basePackages = "org.openhubframework.openhub.ri.persistence.repository") @Configuration public class SecondDataSourceJpaConfiguration { @Bean @Qualifier("customTransactionManager") PlatformTransactionManager customTransactionManager() { return new JpaTransactionManager(customEntityManagerFactory().getObject()); } @Bean @Qualifier("customEntityManagerFactory") LocalContainerEntityManagerFactoryBean customEntityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(false); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); factoryBean.setPackagesToScan(User.class.getPackage().getName()); factoryBean.setDataSource(customDataSource()); return factoryBean; } @Bean @Qualifier("customDataSource") DataSource customDataSource() { return DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/testdb?useSSL=false&nullNamePatternMatchesAll=true") .driverClassName("com.mysql.jdbc.Driver") .username("*****") .password("*****") .build(); } |