java - How to handle an Entity in JPA with Single Instance? -


hi im new jpa want know how handle following scenario

i need maintain in database total count of messages received, there no need store messages count.im representing count entity called messagecount having variable count follows

    @entity       public class messagecount       {           long count;     ... 

i need update count whenever recieve message.

how can update ?
correct approach handle ?

thanks in advance .

the way reliably (read: atomically) issue direct query (as opposed fetching entity , incrementing it). appropriate jpa query be:

update messagecount set count = count + 1 

if fetch entity jpa, add 1, , save again - risk overwriting value thread has incremented , saved. performing operation in single query ensures happens in single, atomic transaction.


Comments