Hibernate 冬眠和;“最大价值赢”;逻辑

Hibernate 冬眠和;“最大价值赢”;逻辑,hibernate,database-design,optimistic-locking,Hibernate,Database Design,Optimistic Locking,我的数据库中有一个high_scores表,它有两个值: player_id highest_score 我需要一个存储库方法来保存一个新的高分。我真的不想为了这么简单的事情而用乐观锁重试逻辑弄脏我的代码。我想执行的基本上是这一行: update high_scores set highest_score = :new_high_score where player_id = :player_id and highest_score <= :new_high_score 更新高分 设置

我的数据库中有一个high_scores表,它有两个值:

player_id
highest_score
我需要一个存储库方法来保存一个新的高分。我真的不想为了这么简单的事情而用乐观锁重试逻辑弄脏我的代码。我想执行的基本上是这一行:

update high_scores
set highest_score = :new_high_score
where player_id = :player_id
and highest_score <= :new_high_score
更新高分
设置最高分数=:新的最高分数
其中player\u id=:player\u id

hibernate支持批量更新(从3.3开始)

编辑:

检查:

更简单(来自Hibernate文档):


干杯

酷。我仍然需要一些额外的东西,以涵盖创造他们的第一个记录的高分,但旁白,这看起来很棒。谢谢
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
        .setString( "newName", newName )
        .setString( "oldName", oldName )
        .executeUpdate();
tx.commit();
session.close();