gpt4 book ai didi

java - JPA 2.1、Hibernate 4.3、Spring 4.0 数据未保存到数据库

转载 作者:行者123 更新时间:2023-11-29 08:50:23 25 4
gpt4 key购买 nike

我有一个使用 spring 4.0、JPA 2.1、Hibernate 4.3.5、Glassfish 4.0 和 JTA 事务管理器的应用程序。
在我将 JPA 从 2.0 更新到 2.1 以及从 Hibernate 4.2.x 更新到 4.3 之后,尽管日志看起来没问题,但数据不再保存到数据库中。
所以这些可能是配置文件:实体管理器

<bean id="emfCC" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="CCDS" />
<property name="packagesToScan" value="com.videanuadrian.core.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>

<property name="jpaProperties">
<props>
<prop key="hibernate.connection.autocommit">false </prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.ehcache.EhCacheProvider</prop>
<prop key="hibernate.generate_statistics">false</prop>
</props>
</property>

我的交易经理:

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:appserver/TransactionManager"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes >
<tx:method name="get*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="true"/>
<tx:method name="is*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>

<aop:aspectj-autoproxy/>
<aop:config>
<aop:pointcut id="cs" expression="execution(* com.videanuadrian.contactcenter.services..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="cs" />
</aop:config>

<aop:config>
<aop:pointcut id="pcCore" expression="execution(* com.videanuadrian.core.impl.services..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pcCore"/>
</aop:config>
<jee:jndi-lookup id="CCDS" jndi-name="jdbc/contactCenter"/>

我所有的 DAO 类都继承了这个类:

public abstract class GenericDAOImpl<T> implements GenericDAO<T> {

@PersistenceContext(type = PersistenceContextType.TRANSACTION,synchronization=SynchronizationType.SYNCHRONIZED)
protected EntityManager em;

private Class<T> type;

protected static final Logger logger = Logger.getLogger(GenericDAOImpl.class);

public GenericDAOImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class) pt.getActualTypeArguments()[0];
}

public EntityManager getEm() {
return em;
}

public void setEm(EntityManager em) {
this.em = em;
}

@Override
public T create(final T t) {
this.em.persist(t);
return t;
}

@Override
public void delete(final Object id) {
this.em.remove(this.em.getReference(type, id));
}

@Override
public T find(final Object id) {
return (T) this.em.find(type, id);
}

@Override
public T update(final T t) {
return this.em.merge(t);
}

@Override
public void flush(){
this.em.flush();
}
}

这是我的服务等级:

@Named
public class ApplicationServiceImpl implements ApplicationService {

@Inject
private ApplicationDAO applicationDAO;


@Override
public Long updateApplication(Application app) {

if (app == null)
return null;

applicationDAO.update(app);

return app.getId();

}
}

在我点击 updateApplication 方法后,日志是正确的,一切似乎都工作正常,但更新不会持久保存到数据库中。起初这似乎没有交易或交易没有创建,但这里是日志:

AbstractPlatformTransactionManager.java:367) - Creating new transaction with name [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,-java.lang.Exception
(TransactionSynchronizationManager.java:272) - Initializing transaction synchronization
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]
(EntityManagerFactoryUtils.java:272) - Opening JPA EntityManager
(EntityManagerFactoryUtils.java:290) - Registering transaction synchronization for JPA EntityManager
(TransactionSynchronizationManager.java:193) - Bound value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] to thread [http-listener-1(3)]
(AbstractBeanFactory.java:249) - Returning cached instance of singleton bean 'auditLogAspectImpl'
(AbstractPlatformTransactionManager.java:472) - Participating in existing transaction
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.UserServiceImpl.getUser]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.UserServiceImpl.getUser]
(AbstractPlatformTransactionManager.java:472) - Participating in existing transaction
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.AuditLogServiceImpl.addAuditLogEvent]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.AuditLogServiceImpl.addAuditLogEvent]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]
(AbstractPlatformTransactionManager.java:926) - Triggering beforeCommit synchronization
(AbstractPlatformTransactionManager.java:939) - Triggering beforeCompletion synchronization
(TransactionSynchronizationManager.java:243) - Removed value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] from thread [http-listener-1(3)]
(EntityManagerFactoryUtils.java:435) - Closing JPA EntityManager
(AbstractPlatformTransactionManager.java:755) - Initiating transaction commit
(AbstractPlatformTransactionManager.java:952) - Triggering afterCommit synchronization
(AbstractPlatformTransactionManager.java:968) - Triggering afterCompletion synchronization
(TransactionSynchronizationManager.java:331) - Clearing transaction synchronization
(RequestLoggingPhaseListener.java:42) - Entering JSF Phase: RESTORE_VIEW 1
(RequestLoggingPhaseListener.java:42) - Entering JSF Phase: RENDER_RESPONSE 6

如果我启用 sql_log,我将看不到正在执行的更新语句....我怀疑 JPA 2.1 与我的实体管理器、事务管理器之间存在某些问题,但我不知道它是什么...
对此有什么想法吗?

最佳答案

我应用的解决方案是取消 persistence.xml 文件并在其中添加属性:

<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />

发现于: https://coderwall.com/p/e5fxrw
我还从我的 EntityManager 定义中删除了 packagesToScan。

关于java - JPA 2.1、Hibernate 4.3、Spring 4.0 数据未保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com