我一直在通过使用以下方式实现JavaEE Web应用程序来自学Spring™。
我的问题是:
根据日志,服务方法在交易开始之前返回!
[org.springframework.orm.jpa.JpaTransactionManager] - <Using transaction object [org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject@335f5e3a]>
[org.springframework.orm.jpa.JpaTransactionManager] - <Creating new transaction with name [org.snowjak.livesavegive.data.service.TagService.rename]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT>
[org.springframework.orm.jpa.JpaTransactionManager] - <Opened new EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@3a234c2f] for JPA transaction>
[org.springframework.orm.jpa.JpaTransactionManager] - <Exposing JPA transaction as JDBC transaction [SimpleConnectionHandle: com.mysql.jdbc.JDBC4Connection@49116f61]>
[myproject.data.service.MyEntityService] - <Beginning method: rename>
[myproject.data.dao.MyEntityDao] - <Beginning method: get>
[myproject.data.dao.MyEntityDao] - <After method: get>
[myproject.data.dao.AbstractDao] - <Beginning method: update>
[myproject.data.dao.AbstractDao] - <After method: update>
[myproject.data.service.MyEntityService] - <After method: rename>
[org.springframework.orm.jpa.JpaTransactionManager] - <Triggering beforeCommit synchronization>
[org.springframework.orm.jpa.JpaTransactionManager] - <Triggering beforeCompletion synchronization>
[org.springframework.orm.jpa.JpaTransactionManager] - <Initiating transaction commit>
[org.springframework.orm.jpa.JpaTransactionManager] - <Committing JPA transaction on EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@3a234c2f]>
[org.springframework.orm.jpa.JpaTransactionManager] - <Triggering afterCommit synchronization>
[org.springframework.orm.jpa.JpaTransactionManager] - <Triggering afterCompletion synchronization>
[org.springframework.orm.jpa.JpaTransactionManager] - <Closing JPA EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@3a234c2f] after transaction>
击> 如果我的DAO是事务性的,而不是我的服务层,则无法更新无效。
任何人都可以提供公会的任何指针/提示/技巧/秘密吗?
击><击> 撞击>
我在记录器中打开了org.springframework的DEBUG
详细信息;并且从我所知道的情况来看,交易正在被正确处理 - 即在相关服务方法之前和之后开始。
我仍然有更新问题,即。我的更新没有出现在PU和DB中;现在为什么会这样? ...
我在进行更新尝试时也偷看了MySQL通用日志。看来,JPA根本就不是向数据库发出UPDATE
!
=============================================== ===================================
在myproject.data.entity
下。没什么特别的。通用JPA。
@Entity
public class MyEntity implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
int id;
@Column(nullable=false, unique=true)
String name;
...
}
myproject.data.dao
下的界面。
interface AbstractDao
@Repository
interface AbstractDao<T,K> extends Serializable {
public T get(K key);
public Collection<T> getAll();
public T save(T entity);
public T update(T entity);
}
interface MyEntityDao
@Repository
interface MyEntityDao extends AbstractDao<MyEntity, Integer> {
public MyEntity get(String name);
}
myproject.data.dao.jpa
下的实施。
abstract class AbstractDaoJpa
@Repository
abstract class AbstractDaoJpa implements AbstractDao<T,K> {
protected EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
em = entityManager;
}
public T save(T entity) {
em.persist(entity);
return entity;
}
public T update(T entity) {
return em.merge(entity);
}
}
class MyEntityDaoJpa extends AbstractDaoJpa implements MyEntityDao
@Repository
class MyEntityDaoJpa extends AbstractDaoJpa<MyEntity,Integer> implements MyEntityDao {
@Override
public MyEntity get(Integer key) {
return em.find(MyEntity.class, key);
}
@Override
public MyEntity get(String name) {
return em.createQuery("select E from MyEntity E where E.name = :name", MyEntity.class).setParameter("name", name).getSingleResult();
}
...
}
myproject.data.service
interface EntityService
@Service
public interface EntityService<T> extends Serializable {
public Collection<T> getAll();
}
interface MyEntityService extends EntityService
@Service
public interface MyEntityService extends EntityService<MyEntity> {
public MyEntity create(String name);
public MyEntity get(String name);
public MyEntity rename(String name);
...
}
myproject.data.service.impl
class MyEntityServiceImpl implements MyEntityService
@Service
public class MyEntityServiceImpl implements MyEntityService {
private MyEntityDao dao;
public MyEntityServiceImpl(MyEntityDao newDao) {
dao = newDao;
}
@Override
public MyEntity create(String name) {
MyEntity e = new MyEntity();
e.name = name;
...
return dao.save(e);
}
...
@Override
public MyEntity rename(String oldName, String newName) {
MyEntity e = dao.get(oldName);
e.name = newName;
dao.update(e); // An attempt to fix by explicitly merging the modified entity.
// Didn't fix the problem.
return e;
}
...
}
...
[ Declaration of DAOs and Service beans. Appropriate DAOs are injected into Service beans. ]
...
<!-- Tomcat-managed DataSource lookup via JNDI -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDS"/>
<!-- Configure the EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
</property>
</bean>
<!-- Create the Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Configure the Transaction Manager for AOP -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- Some methods are read-only. -->
<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
<!-- All others, use defaults. -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
...
<!-- Configure Spring AOP -->
<aop:config>
<aop:pointcut id="wholeProject" expression="within(myproject..*)"/>
<aop:pointcut id="serviceLayerOperation" expression="execution(* myproject.data.service.*.*(..))"/>
<!-- Configure transaction management. -->
<aop:advisor advice-ref="txAdvice" order="1" pointcut-ref="serviceLayerOperation"/>
<!-- Configure logging interceptor. -->
...
</aop:config>
答案 0 :(得分:0)
问题来自于您直接访问实体的字段而无需通过getter / setter。
使用公共字段是一种非常糟糕的做法,因为它破坏了封装。 EclipseLink似乎需要方法,可能是因为编织包括在方法中添加字节码。通过直接访问字段,您必须绕过使EclipseLink工作的字节码添加。