Spring JPA 抛出 java.lang.IllegalArgumentException:不是从依赖 jar 加载的类的托管类型异常

问题描述

我正在使用 springmvc+jpa(无 spring-boot)代码库,我们将一些实体类打包在一个带有相应 hbm 映射文件的 jar 中,并作为依赖添加到当前项目中,我们有两个事务管理器,一个是基于 Hibernate 的(可以正常工作),另一个是基于 jpa entityManager 的,我配置如下

@Configuration
@EnableJpaRepositories( basePackages = "<repopackage>",bootstrapMode = BootstrapMode.LAZY)
public class DataSourceConfig {
    
    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerfactorybean entityManagerfactorybean(DataSource dataSource,MultiTenantConnectionProvider multiTenantConnectionProvider,CurrentTenantIdentifierResolver currentTenantIdentifierResolver) {
        
        Map<String,Object> props = new HashMap<String,Object>();
        props.put(Environment.MULTI_TENANT,"DATABASE");
        props.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER,currentTenantIdentifierResolver);
        props.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER,multiTenantConnectionProvider);
        props.put(Environment.DIALECT,"com.custom.PostgresqlDialect");
        props.put(Environment.SCANNER_disCOVERY,"class,hbm");
        props.put(Environment.ENABLE_LAZY_LOAD_NO_TRANS,"true");
        props.put(Environment.SHOW_sql,"true");
        LocalContainerEntityManagerfactorybean entityManagerFactory = new LocalContainerEntityManagerfactorybean();
        entityManagerFactory.setPackagesToScan(new String[] { "<domain package in current project>","domain package in dependency jar" });
        HibernateJpavendorAdapter apt = new HibernateJpavendorAdapter();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpavendorAdapter(apt);
        entityManagerFactory.setJpaPropertyMap(props);
        return entityManagerFactory; 
    }

    @Bean(name="transactionManager") 
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
}

该配置适用于当前项目中带有注释的实体类,我可以创建实现 JpaRepository 接口的存储库并能够访问它。现在,当我为依赖 jar 中存在的域对象创建存储库时,我收到此异常

Request processing Failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;resourceDetailRepository&#39;: 
factorybean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.ResourceDetail
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)