0
我需要將圖像上傳到數據庫(不確定是否這是一個好主意或者上傳文件並從數據庫中引用它),並且我已經定義了一個表單和一個控制器來做到這一點。問題是我需要將MultipartFile轉換爲Blob對象,爲此我需要使用需要Session對象的Hibernate.getLobCreator。在基於Java的配置的Spring App上獲取SessionFactory
這是使用第一次基於Java的配置,我有下一個類定義的持久化上下文:
@Configuration
@EnableJpaRepositories(basePackages = {
"com.davidmogar.alsa"
})
@EnableTransactionManagement
public class PersistenceContext {
private static final String[] ENTITY_PACKAGES = {
"com.davidmogar.alsa.domain"
};
private static final String PROPERTY_NAME_DB_DRIVER_CLASS = "db.driver";
private static final String PROPERTY_NAME_DB_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DB_URL = "db.url";
private static final String PROPERTY_NAME_DB_USER = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
@Bean(destroyMethod = "close")
DataSource dataSource(Environment environment) {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(environment.getRequiredProperty(PROPERTY_NAME_DB_DRIVER_CLASS));
dataSourceConfig.setJdbcUrl(environment.getRequiredProperty(PROPERTY_NAME_DB_URL));
dataSourceConfig.setUsername(environment.getRequiredProperty(PROPERTY_NAME_DB_USER));
dataSourceConfig.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DB_PASSWORD));
return new HikariDataSource(dataSourceConfig);
}
@Bean(name = "entityManagerFactory")
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, Environment environment) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan(ENTITY_PACKAGES);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty
(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, environment.getRequiredProperty
(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty
(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty
(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty
(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
我怎樣才能獲得會話控制器到MultipartFile轉換?
爲什麼不簡單地使用一個包含'byte []'作爲文件內容的hibernate託管對象,這可以作爲一個blob自動存儲。不需要自己搞砸。 –