我有一个使用 Spring 3.2.3 和 Hibernate 4.2.1.Final 的应用程序。我做了一些配置,应用程序在测试环境中运行良好,使用 HSQLDB 等。
但是当应用程序被部署时,它几乎可以正常工作。实体已创建但从未持久化。我可以看到 JPA 日志:
休眠:选择 nextval ('TASK_SEQ')
但是插入永远不会出现=(
使用配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="br.com.company" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="spring_pu" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="spring_pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.company.core.entities.Task</class>
<properties>
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.url" value="jdbc:postgresql://127.0.0.1:5432/companyDB" />
<property name="hibernate.connection.user" value="postgres" />
<property name="hibernate.connection.password" value="*****" />
</properties>
</persistence-unit>
</persistence>
实体:
@Entity
@Table(name = "TASK")
public class Task implements Serializable {
private static final long serialVersionUID = -6262731134419520342L;
@Id
@Column(name = "ID")
@GeneratedValue(generator = "TASK_SEQ", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(sequenceName = "TASK_SEQ", name = "TASK_SEQ")
private long id;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "FINISHED")
private boolean fininshed;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "FINISH_DATE")
private Date finishDate;
//getters and setter below
}
最后是服务:
@Service
public class TaskService {
@PersistenceContext
private EntityManager entityManager;
@Transactional(propagation = Propagation.REQUIRED)
public void createTask(Task task) {
<b>//invoked method</b>
entityManager.persist(task);
}
正如我所说,没有抛出异常,但实体不会像在测试中那样持久化。谢谢!
编辑:我还尝试将persistence.xml内容删除到spring数据源中,问题仍然相同:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/KCILDS" />
<property name="username" value="postgres" />
<property name="password" value="*****" />
</bean>
<context:component-scan base-package="br.com.company" />
<bean id="myEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.company.core.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEntityManagerFactory" />
</bean>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
解决方案:
我放弃了xml配置。似乎没有什么可以再使用它了。阅读更多关于趋势和大量配置的信息,我最终成功地尝试了一个 java 配置,并进行了一些调整,这将非常适合。见下文:
@Configuration
@EnableTransactionManagement
@ComponentScan("br.com.company")
public class PersistenceJPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(this.directDataSource());
factoryBean.setPackagesToScan(new String[] { "br.com.company" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(true);
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setJpaProperties(this.additionlProperties());
return factoryBean;
}
private Properties additionlProperties() {
Properties properties = new Properties();
properties.put("database", "POSTGRESQL");
properties.put("databasePlatform", "org.hibernate.dialect.PostgreSQLDialect");
properties.put(Environment.SHOW_SQL, true);
properties.put(Environment.FORMAT_SQL, true);
return properties;
}
// now reasearch how to make it an environment configuration
// @Bean
// public DataSource dataSource() {
// JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
// jndiDataSourceLookup.setResourceRef(true);
// return jndiDataSourceLookup.getDataSource("jdbc/mybank");
// }
@Bean
public DataSource directDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/MyBank");
dataSource.setUsername("postgres");
dataSource.setPassword("*******");
return dataSource;
}
@Bean //still trying to make a JTA Transaction
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(this.entityManagerFactoryBean().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}