我从Hibernate 5.0迁移到5.2,目前面临来自其他jar的加载映射问题。我想创建一个完全没有XML的代码,因此,需要以编程方式配置Hibernate。这是我的HibernateConfiguration:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:core.properties" })
@ComponentScan({ "com.test.core.domain" })
public class HibernateConfiguration {
private static Logger logger = Logger.getLogger(HibernateConfiguration.class);
private final static String HIBERNATE_DIALECT = "org.hibernate.dialect.PostgreSQL95Dialect";
@Autowired private Environment env;
@Autowired private ResourceLoader resourceLoader;
@Bean
public SessionFactory sessionFactory() throws IOException {
StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySettings(additionalProperties());
standardServiceRegistryBuilder.applySetting("hibernate.connection.datasource",dataSource());
MetadataSources metadataSources = new MetadataSources(standardServiceRegistryBuilder.build());
Resource resource = resourceLoader.getResource("classpath*:/hibernate");
File file = resource.getFile();
Iterator<File> fi = FileUtils.iterateFiles(file, null, true);
while(fi.hasNext()) {
metadataSources.addFile(fi.next());
logger.info("URL to hibernate is: " + fi.next());
}
Metadata metadata = metadataSources.getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
}
@Bean
public DataSource dataSource() {
ComboPooledDataSource сomboPooledDataSource = new ComboPooledDataSource();
try {
сomboPooledDataSource.setDriverClass(Preconditions.checkNotNull(env.getProperty("jdbc.driver-class-name")));
} catch( PropertyVetoException pve ){
logger.error("Cannot load datasource driver (" + env.getProperty("jdbc.driver-class-name") +"): " + pve.getMessage());
return null;
}
сomboPooledDataSource.setJdbcUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
сomboPooledDataSource.setUser(Preconditions.checkNotNull(env.getProperty("jdbc.username")));
сomboPooledDataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.password")));
сomboPooledDataSource.setMinPoolSize(20);
сomboPooledDataSource.setMaxPoolSize(50);
сomboPooledDataSource.setCheckoutTimeout(15);
сomboPooledDataSource.setMaxStatements(0);
сomboPooledDataSource.setIdleConnectionTestPeriod(30);
return сomboPooledDataSource;
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) throws Exception{
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
private static Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", HIBERNATE_DIALECT);
properties.setProperty("hibernate.bytecode.use_reflection_optimizer", "true");
properties.setProperty("hibernate.show_sql", "false");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.default_batch_fetch_size", "1000");
properties.setProperty("hibernate.max_fetch_depth", "2");
properties.setProperty("hibernate.generate_statistics", "false");
properties.setProperty("hibernate.default_schema", "EDRIVE");
properties.setProperty("hibernate.connection.CharSet", "utf8");
properties.setProperty("hibernate.connection.characterEncoding", "utf8");
properties.setProperty("hibernate.connection.useUnicode", "true");
properties.setProperty("hibernate.connection.release_mode", "after_transaction");
properties.setProperty("hibernate.jdbc.batch_size", "50");
properties.setProperty("hibernate.jdbc.fetch_size", "500");
properties.setProperty("hibernate.jdbc.use_scrollable_resultset", "false");
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
properties.setProperty("hibernate.cache.use_query_cache", "true");
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.use_structured_entries", "false");
properties.setProperty("hibernate.current.session.context.class", "org.springframework.orm.hibernate5.SpringSessionContext");
return properties;
}
}
当前实现抛出FileNotFoundException:
java.io.FileNotFoundException:ServletContext资源 [/ classpath *:/ hibernate]无法解析为绝对文件路径 - Web应用程序存档未扩展?
所以,问题是 - 如何添加我的映射* .hbm.xml存储在其他jar中?