休眠和“最大价值获胜”逻辑

| 我的数据库中有一个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
尽管我也不得不担心如果用户没有一个高分,那么我将不得不担心要创造一个高分,这基本上可以很好地实现我的目标。我需要确保我永远不会用较低的分数覆盖较高的分数,但这是该表所需的唯一锁定方式。 有没有办法让冬眠做类似的事情,还是我需要求助于自定义SQL?     
已邀请:
hibernate支持批量更新(从3.3开始)。 编辑: 校验: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-direct 使它变得更容易(来自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();
干杯!     

要回复问题请先登录注册