gpt4 book ai didi

java - Hql查询类似于hibernate中的SaveOrUpdate

转载 作者:行者123 更新时间:2023-12-01 04:49:25 26 4
gpt4 key购买 nike

当我们使用Session时,Hibernate中有saveOrUpdate(Object)函数,并且它更容易,因为我们不必检查是否执行保存或更新。但 insert(Object)update(Object) 函数仅适用于无状态 session ,当我处理数十万条记录时,我需要使用无状态 session ,并且 hibernate 正在消耗内存。有没有办法在无状态 session 中复制 saveOrUpdate(Object) 功能。或者无论如何使用 Hql 查询而不是使用对象通过 hibernate 来完成此操作。

最佳答案

根据StatelessSession JavaDoc

A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors. Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache

它不维护一级缓存,因此不知道对象的当前状态( transient 、持久、分离或删除),因此它无法做出有意识的决定。您可以使用 HQL 查询数据库中是否存在相同的记录,并调用相应的方法 insert()update()。但这是代价高昂的。

如果有数十万条记录需要更新或插入,最好使用基本 JDBC 而不是 hibernate。这将解决你的内存问题。

关于java - Hql查询类似于hibernate中的SaveOrUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241233/

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