database design - Hibernate and "greatest value wins" logic -
i have high_scores table in database, has 2 values:
player_id highest_score
i need repository method saves new high score. don't want dirty code optimistic lock retry logic simple. i'd executed line:
update high_scores set highest_score = :new_high_score player_id = :player_id , highest_score <= :new_high_score
that accomplish goal fine, although i'd have worry creating high score if user didn't have one. need make sure never overwrite high score lower score, that's sort of locking table needs.
is there way ask hibernate this, or need resort custom sql?
hibernate supports bulk update (from 3.3 on).
edit:
check: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-direct
making easier (from hibernate docs):
session session = sessionfactory.opensession(); transaction tx = session.begintransaction(); string hqlupdate = "update customer c set c.name = :newname c.name = :oldname"; // or string hqlupdate = "update customer set name = :newname name = :oldname"; int updatedentities = s.createquery( hqlupdate ) .setstring( "newname", newname ) .setstring( "oldname", oldname ) .executeupdate(); tx.commit(); session.close();
cheers!
Comments
Post a Comment