我需要将图像上传到数据库(不确定是否最好这样做或上传文件并从数据库中引用它)并且我已经定义了一个表单和一个控制器来执行该操作。问题是我需要将MultipartFile转换为Blob对象,为此我需要使用需要Session对象的Hibernate.getLobCreator。
这是第一次使用基于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?
答案 0 :(得分:1)
您可以通过EntityManager获取Hibernate Session的实例。
@Autowired
private EntityManagerFactory emf;
public void foo(){
EntityManager em = emf.createEntityManager();
Session session = em.unwrap(Session.class);
LobCreator lob = getLobCreator(session);
...
}