我有一个 PSU 实体:
@Entity
public class Psu extends PanacheEntityBase {
@Id
@GeneratedValue
public UUID uuid;
@Column(nullable = false, length = 50)
public String username;
public Integer coins;
public String firebaseRegistrationToken;
public void setCoins(int coins){
this.coins = coins;
}
public int getCoins(){
return coins;
}
public static Psu findByUsername(String username){
return find("username", username).firstResult();
}
}
我的 PsuService 类中的一个函数:
@Transactional
public Transaction changeBalance(Transaction transaction){
Psu psuSender = Psu.findById(transaction.senderId);
psuSender.coins = psuSender.coins - transaction.amount;
Psu.persist(psuSender);
transaction.status = TransactionStatus.PROCESSED;
return transaction;
}
当我在函数调用之后记录所有用户及其硬币数量时,它表明硬币已为正确的人更新:
List<Psu> allPsu = Psu.findAll().list();
for(Psu psu : allPsu){
System.out.println("ID: " + psu.uuid + " coins: " + psu.coins);
}
当我通过 API 检索更新用户的余额时:
@GET
@Path("psu/getBalance/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
public String getBalanceById(@PathParam("uuid") UUID uuid) {
List<Psu> allPsu = Psu.findAll().list();
for(Psu psu : allPsu){
System.out.println("ID: " + psu.uuid + " coins: " + psu.coins);
}
return gson.toJson(psuService.getBalanceById(uuid));
}
在 PsuService 类中使用此方法:
@Transactional
public int getBalanceById(UUID uuid){
Psu psu = Psu.findById(uuid);
return psu.getCoins();
}
然后它返回未更新的旧余额。
有谁知道为什么我不能更新余额?
- PsuService 扩展了 PanacheEntityBase
- PsuService 是 @ApplicationScoped
编辑 进行更新时:
Psu.update("coins = ?1 where id= ?2", 10, transaction.senderId);
我收到错误:
ERROR [io.qua.sch.run.SimpleScheduler] (executor-thread-1) Error occured while executing task for trigger IntervalTrigger [id=1_com.msp.sqs.MessageQueue_ScheduledInvoker_receiveMessages_e721e20dd5fdaa8a94c141
a7768b0311d6f32352, interval=1000]: javax.persistence.TransactionRequiredException: Executing an update/delete query