【发布时间】:2014-08-05 06:04:10
【问题描述】:
我的主要目标是尽可能多地利用 Spring Boot 应用程序基础设施来读取文件并将一些对象保存到数据库中,经过大量处理。问题是处理发生了,找到了DAO并执行了entityManager.save(),但是没有对象被持久化(没有运行hibernate SQL)。
这是我的加载工具应用程序(可能是错误的):
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.touchcorp.touchpoint"})
@Transactional
public class ContentImportingApplication {
@Autowired
private VoucherTemplateDao voucherTemplateDao;
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(ContentImportingApplication.class);
// ... customize app settings here
ApplicationContext ctx = app.run(args);
ContentImporter importer = new ContentImporter(ctx);
importer.importContent();
System.exit(0);
}
private static class ContentImporter {
private ApplicationContext ctx = null;
public ContentImporter(ApplicationContext ctx) {
this.ctx = ctx;
}
public void importContent() throws Exception {
String otc = readFileAsString("content-to-import");
SAXBuilder sb = new SAXBuilder();
Document jDom = sb.build(new StringReader(otc));
// lots of processng code here .. this all works
// ....
Set<Long> productIds = productDocuments.keySet();
VoucherTemplateDao dao = (VoucherTemplateDao) ctx.getBean("voucherTemplateDao");
System.out.println("do we have a dao?: " + dao);
for(Long productId : productIds) {
VoucherTemplate vt = VoucherTemplate.make(productId, "RETAILER_GROUP:*|CHANNEL:*|LOCALE:de-AT|INDUSTRY:5499", VoucherTemplate.TemplateSchema.OTC);
vt.setTemplate(productDocuments.get( productId ));
// the thing that won't persist
dao.save(vt);
}
jDom.getRootElement().addContent( new Element("w") );
}
private void findAndReplaceNestedDocs(Document jDom, Element documentInOtcContent) {
// ...code omitted: this all works too...
}
public String readFileAsString(String name) {
// ...code omitted: and so does this...
}
}
}
它利用了各种配置类,但我认为 PersistenceConfig 是最重要的:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan(basePackages = {"com.xxxxxcorp.xxxxxpoint.model"})
public class PersistenceConfig
{
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.xxxxxcorp.xxxxxpoint.model.domain" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaDialect(jpaDialect());
em.setJpaProperties(hibernateProperties());
return em;
}
@Bean
public DataSource dataSource()
{
// assumes dbcp
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory( this.entityManagerFactory().getObject() );
transactionManager.setJpaDialect(this.jpaDialect());
return transactionManager;
}
@Bean
public JpaDialect jpaDialect() {
return new HibernateJpaDialect();
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
{
return new PersistenceExceptionTranslationPostProcessor();
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", "true");
}
};
}
}
相关的属性文件:
# jdbc
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxxxxxpoint?autoReconnect=true
jdbc.user=root
jdbc.pass=password
# hibernate
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=verify
正如我所说,所有这些都运行时没有错误,也没有持久性。顺便说一句,我已经用主应用程序类祝福了 pom(在属性下):
<start-class>com.xxxxxcorp.xxxxxpoint.Application</start-class>
所以我确定我不会启动两个上下文或两个应用程序或任何其他类似的东西。谁能告诉我哪里出错了?
【问题讨论】:
标签: hibernate jpa spring-boot