Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring事务更新实体_Java_Spring_Transactions - Fatal编程技术网

Java Spring事务更新实体

Java Spring事务更新实体,java,spring,transactions,Java,Spring,Transactions,我有一个问题。我创建了基本Reposiotry: @Transactional public abstract class BaseRepository<T extends AbstractEntity> { public T read(Long id , Class<T> classEntity) { if(id == null || classEntity == null) { throw new NullPointer

我有一个问题。我创建了基本Reposiotry:

@Transactional
public abstract class BaseRepository<T extends AbstractEntity> {
    public T read(Long id , Class<T> classEntity) {
        if(id == null || classEntity == null) {
            throw new NullPointerException();
        }
        T result = entityManager.find(classEntity , id);
        return result;
    }

}
我使用OpenEntityManagerViewFilter。这个片段来自applicationContext.xml

<aop:aspectj-autoproxy/>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <tx:annotation-driven proxy-target-class="true" />
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


最后是问题。在数据库中,我有一个login='AdminUser',Id=1的用户。当我调用方法read-in数据库时,用户在“some”上更改登录名。在日志中,我看到了对数据库的更新请求。我能修好这个吗?

不,你不能修好它。因为有opensessioninviewfilter,所以其中有一个事务。所以,最好的办法是在更改对象之前克隆它。

我不明白。您从数据库中检索一个托管实体,更改其登录名,您会惊讶地发现执行了一个查询来更新数据库中的登录名?如果不想修改登录名,为什么要修改实体的登录名?这不是一个bug,而是一个特性:托管实体是。。。托管:它们的状态由JPA持久化。这就是使用ORM的全部意义。如果我在存储库之外修改实体,而不进行事务性注释,这是所有权利,但我只想为用户准备实体,而不在数据库中进行更改。这是不可能的?
 @Override
    public User prepare(User entity) {
       entity.setLogin("some");
       return entity;
}
<aop:aspectj-autoproxy/>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <tx:annotation-driven proxy-target-class="true" />
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>