2016-11-06 60 views
3

我使用Spring引導數據JPA和Tomcat啓動器導入時錯誤PersistenceUnitRootLocation URL,PersistenceUnit在JAR文件中配置在web應用程序上下文

我有幾個Maven的模塊定義多個持久化單元,其正在申請正確使用獨立:

Project 
-- IdentityAccess [PU name: identity-access] 
|--- src/main/java 
     |--- myapp.application [Application services using "identityAccessTransactionManager"] 
     |--- myapp.domain.model [Entites classes] 
     |--- myapp.infrastructure.persistence [Repositories Impl using nested DataJpa repositories] 
|--- src/main/resources 
     |--- myapp/application.properties 
     |--- myapp/hibernate.cfg.xml 
     |--- myapp/infrastructure/persistence/Group.hbm.xml 
     |--- myapp/infrastructure/persistence/GroupMember.hbm.xml 
     |--- myapp/infrastructure/persistence/User.hbm.xml 

-- Promotion [PU name: promotion] 
... 

-- WebApplication [There is no PU here] 
-> Depends on IdentityAccess, Promotion 

@SpringBootApplication 
@Import({IdentityAccessContext.class, PromotionContext.class}) 

這是我如何配置我的每個PU EntityManagerFactoryBean給他們一個獨特的bean名稱,以避免衝突。 注意:下面的配置是包含在每個模塊

@Configuration 
@ComponentScan 
@EnableJpaRepositories(
     transactionManagerRef = TRANSACTION_MANAGER, 
     entityManagerFactoryRef = ENTITY_MANAGER_FACTORY, 
     considerNestedRepositories = true) 
public class IdentityAccessPersistenceConfiguration { 
    public static final String PERSISTENCE_CONTEXT = "identity-access"; 
    public static final String ENTITY_MANAGER_FACTORY = "identityAccessEntityManagerFactory"; 
    public static final String TRANSACTION_MANAGER = "identityAccessTransactionManager"; 

    @Bean 
    public LocalContainerEntityManagerFactoryBean identityAccessEntityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { 
     LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactory.setDataSource(dataSource); 
     entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter); 
     entityManagerFactory.setMappingResources("myapp/identityaccess/hibernate.cfg.xml"); 
     entityManagerFactory.setPersistenceUnitName("identity-access"); 
     return entityManagerFactory; 
    } 

    @Bean 
    public PlatformTransactionManager identityAccessTransactionManager(EntityManagerFactory entityManagerFactory) { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory); 
     return transactionManager; 
    } 

} 

當我單獨啓動每個模塊,即,在測試情況下也可作爲獨立的應用程序,entites的裝載和正確映射的一類,所述PersistenceUnitInfoDescriptor有一個根URL是這樣的:

file:/F:/Workspaces/myapp/IdentityAccess/target/classes/ 

但是,當我啓動web應用程序,每個PU帶有另一根URL,這是web應用上下文的根,因而它不能加載映射類:

file:/F:/Workspaces/myapp/WebApplication/target/WebApplication-1.0.0-SNAPSHOT/WEB-INF/classes/ 

它應該不是包含PU的罐子嗎?我試圖鉤住setPersistenceUnitRootLocation來放置我的jar網址,但由於版本和路徑可以明顯改變,所以我被卡住了。我甚至不知道它是一個錯誤還是我正在做一些不被支持的事情。也許這個bug甚至不屬於Spring Boot,而是Spring本身。

我感到我觸摸有點太多的內部,這就是爲什麼我認爲這是一個錯誤,但請糾正我,如果不是我在用相同的描述春季啓動github上已經開了罰單。

我也看了看: - https://github.com/spring-projects/spring-boot/issues/6983 - https://github.com/spring-projects/spring-boot/issues/6635 - https://github.com/spring-projects/spring-boot/issues/7003 - https://github.com/spring-projects/spring-boot/issues/7021

這似乎有關。

謝謝您的時間,

回答

0

我解決了這一問題:

在org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration,該方法假設持久性單元是在應用程序本身:

private URL determinePersistenceUnitRootLocation() { 
     Class<?> source = getClass(); 
     try { 
      URL url = source.getProtectionDomain().getCodeSource().getLocation(); 
      return ResourceUtils.extractJarFileURL(url); 
     } 
     catch (Exception ex) { 
      logger.info("Could not determine persistence " + "unit root location from " 
        + source + " : " + ex); 
     } 
     return null; 
    } 

源代碼屬於主要的webapp,所以類加載器放錯了路徑文件。

爲了解決我的問題,這是一個在我的配置文件中使用jar配置類中的相同方法設置RootPersistenceUnitRootLocation,並且該單元已從JAR正確加載的問題。

的問題是:應春開機自動配置使用類從持久性單元來代替,或者可能與setJarUrl發揮包括適當的修正?還是僅僅支持一個主要的持久性單元?我認爲自動配置並允許開箱即用的多個持久性單元(來自不同的jar)尤其適用於域驅動設計體系結構將是非常好的。

相關問題