简体   繁体   中英

How to configure Spring Boot with common Entity class and multiple projects

i have multiple project with common entity classes. So i created jar file of that common entity class and added this jar as library for multiple projects (right click on project -> property->libraries->Add external jars).

common entity folder under package com.company.apmg.entity;

And the multiple project have the folder structure like

`com.company.apmg.usermanagement.v1`;
`com.company.apmg.shippingmanagement.v1`;
`com.company.apmg.billingmanagement.v1`;

here have the controller,service and repository files.

On compiling projects no issues found.

But running application it shows Entity class is Not a managed type. How i can solve this issue...

I referred the google but no one could not solve my issue.

It is that you are creating different persistence units, which in turn creates different entity managers. So, try to scan the entity classes inside your project. For instance, you can add a class PersistenceConfig like below.

@Configuration
@EnableJpaRepositories(basePackages = { "your.package.repositories" })
public class PersistenceConfig {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("your.package.entities");

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

        return em;
    }
@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver"); //com.mysql..cj.* is added on version 8 of the driver
    dataSource.setUrl("jdbc:mysql://localhost:3306/db");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    return dataSource;
}

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

    return transactionManager;
}

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

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

    return properties;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM