微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。