Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Spring HibernateTemplate保存执行插入,但不执行更新_Spring_Hibernate_Transactions - Fatal编程技术网

Spring HibernateTemplate保存执行插入,但不执行更新

Spring HibernateTemplate保存执行插入,但不执行更新,spring,hibernate,transactions,Spring,Hibernate,Transactions,我有一个典型的Spring/Hibernate设置。这是我的spring配置: <context:annotation-config /> <context:component-scan base-package="com.myco.myapp.modules" /> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sessionFactory"

我有一个典型的Spring/Hibernate设置。这是我的spring配置:

<context:annotation-config />

<context:component-scan base-package="com.myco.myapp.modules" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sessionFactory"
...     
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
以及扩展它的存储库类:

@Repository
public class PersonRepositoryImpl extends BaseRepositoryImpl<Person, String>
我调用了下面的方法saveSomeStuff(),当我使用BaseRepository.save()插入时,它可以很好地工作。但当我尝试更新时,它不会进行更改:

@Override
@Transactional
public void saveSomeStuff() {

    try {

        Person existingPerson = _personRespository.findById("1");

        existingPerson.setName("John");

        _personRespository.save(existingPerson);

        Person dbExistingPerson = _personRespository.findById("1");

        // This prints "John".
        System.out.println(dbExistingPerson.getName());

        Person newPerson = new Person();
        newPerson.setName("Jack");
        _personRespository.save(newPerson);

    } catch (RepositoryException e) {
        e1.printStackTrace();

    }
}
我想我可能有一个跨偶然性问题,但正如我所说的,在离开服务方法时,新的人员将被持久保存在数据库中。在日志中,我看到:

插入某人

但是,我所做的更新不会持久化,日志中没有错误,也没有“update”sql语句。我认为HibernateTemplate.save()方法可能是问题所在,但在saveSomeStuff()方法中,从数据库加载人员后,我执行System.out,从数据库加载的人员具有更新的名称


这里我缺少了什么?

有一个单独的方法,
saveOrUpdate(entity)
。如果不希望hibernate在保存时生成id,可以使用它。

保存方法将保存实体。如果不存在标识符,则将分配标识符。如果有人这样做了,它实际上是在进行更新。返回生成的实体ID。

解决了问题。如果我包含了我的实体类,可能会有人比我更早看到它

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Immutable
@Table(name = "PEOPLE")
public class Person {
  ...
}
最初,我收到一个缓存错误:

java.lang.UnsupportedOperationException: Can't write to a readonly object
快速解决方案?添加@Immutable注释。但如果您阅读了相关文档:

An immutable entity may not be updated by the application. 
Updates to an immutable entity will be ignored, but no exception is thrown.
这解释了为什么1)更新被忽略,2)没有引发异常

所以我去掉了@Immutable注释,并将缓存更改为:

 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
现在一切都很好


总之:rtfm。

我偶然发现了同样的问题。实体正在被插入到数据库中,但在更新某些列时没有更新,并且日志中没有错误。在浏览完entity类之后,我发现我已经注释了一些字段,如下所示

@Column(name = "CREATED_DT", updatable = false)
private Date createdOn;

从注释中删除可更新属性后,更新工作正常。

是的,这也不起作用。:)Hibernate似乎足够聪明,可以区分插入和更新。我还认为我需要使用saveOrUpdate()。因为那是医生说要用的。但似乎save()可以很好地执行更新。它就是粘不住!是的,我知道。而且似乎更新正在事务中执行。但一旦超出它,更新就会丢失。没有意义的是,调用相同的方法,并执行插入而不是更新,它工作得非常好。
 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Column(name = "CREATED_DT", updatable = false)
private Date createdOn;