2016-12-16 37 views
1

我有一個使用帶有「CompilingClassLoader」的庫的web應用程序,當它執行重載(因爲某人編輯了代碼)時,每次都會創建一個新的類加載器。發生這種情況時,在一個Hibernate插件,下面的代碼我寫執行hibernate 5和JPA並動態設置實體類加載器

@Singleton 
@Provides 
public EntityManagerFactory providesSessionFactory() throws IOException { 
    log.info("Loading Hibernate. ENTITY classloader="+entityClassLoader+" hibernate classloader="+this.getClass().getClassLoader()); 
    Map<String, Object> properties = new HashMap<>(); 
    properties.put("hibernate.classLoader.application", entityClassLoader); 
    EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnit, properties); 
    log.info("Done loading Hibernate"); 
    return factory; 
} 

出現該問題,因爲我覺得「hibernate.classLoader.application」必須在persistence.xml中而不是定義(這違背了目的每當開發人員更改代碼時,就會發生這種情況)。

基本上,我們有一個開發模式,其中CompilingClassLoader不斷變化(它真的很快,所以它工作得很好),但我現在無法弄清楚如何在hibernate中做到這一點。有沒有辦法通過Thread.current.setContextClassLoader()中的某個ThreadLocal或者SomeR來設置它。

感謝, 院長

回答

1

這結束了工作

Collection<ClassLoader> classLoaders = new ArrayList<>(); 
    classLoaders.add(entityClassLoader); 
    Map<String, Object> properties = new HashMap<>(); 
    properties.put(AvailableSettings.CLASSLOADERS, classLoaders); 

    EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnit, properties);