There are multiple ways in OpenHub how to store custom business data.

See following variants:

a) Adding tables to the OpenHub schema

Steps

  1. Add custom JPA entities
  2. Extend OpenHub database scheme with new objects (tables, sequences ...) . Directly in project database, flyway in OpenHub can be used as well.
  3. Configure EntityManager, to scan custom packages, with OpenHub configuration property ohf.jpa.additional-packages
    Example: 

    ohf.jpa.additional-packages=org.openhubframework.openhub.ri.persistence


  4. Entity manager bean can be used for building custom dao, or configure repositories using spring-data-jpa.

b) Add second datasource to be used without JPA

c) Add second datasource, with global transaction (one transactionManager)

Steps

  1. Add custom JPA entities.
  2. Configure project-specific DataSource & EntityManagerFactory. Each of them should have custom Qualifier.
  3. 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();
        }
    
    


d) Add second datasource, each having its own transactionManager

Steps

  1. Add custom JPA entities.
  2. Configure project-specific DataSource, EntityManagerFactory & TransactionManager. Each of them should have custom Qualifier.
  3. 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


  4. Create custom dao, or configure spring-data-jpa repositories.

    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();
        }