使用 Spring+Hibernate 更新
Update using Spring+Hibernate
我想使用 Hibernate+ 执行事务Spring.I 想从 DB 检索数据并更新参数然后坚持 same.I 我是 hibernate 的新手有人可以评估代码并建议我实现 same.Here 的最佳方法是代码片段:
@Repository
public class WalletDaoImpl implements WalletDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean creditWallet(double amount, final String customerId) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Criteria cr = session.createCriteria(Wallet.class);
cr.add(Restrictions.eq("customerId", customerId));
Wallet walletInfo = (Wallet) cr.uniqueResult();
tx.commit();
final double newbalaance = walletInfo.getAmount() + amount;
walletInfo.setAmount(newbalaance);
Transaction tx2 = session.beginTransaction();
session.update(walletInfo);
tx2.commit();
session.close();
return true;
}
}
提前致谢。
最好的办法是 Spring 交易管理;您应该使用 spring 的 @Transactional 注释并配置 spring 以便启用事务注释
所以,回到你的代码...我会做这样的事情:
@Repository
public class WalletDaoImpl implements WalletDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean creditWallet(double amount, final String customerId) throws Exception
{
Session session = this.sessionFactory.getCurrentSession();
Criteria cr = session.createCriteria(Wallet.class);
cr.add(Restrictions.eq("customerId", customerId));
Wallet walletInfo = (Wallet) cr.uniqueResult();
final double newbalaance = walletInfo.getAmount() + amount;
walletInfo.setAmount(newbalaance);
session.update(walletInfo);
return true;
}
}
@Service
public class WalletServiceImpl implements WalletService
{
@Autowired
private WalletDao dao;
@Override
@Transactional
public boolean updateWallet(double amount, final String customerId) throws Exception
{
return dao.creditWallet(amount, customerId);
}
}
这里有更深入的解释:Spring transaction managment
我想使用 Hibernate+ 执行事务Spring.I 想从 DB 检索数据并更新参数然后坚持 same.I 我是 hibernate 的新手有人可以评估代码并建议我实现 same.Here 的最佳方法是代码片段:
@Repository
public class WalletDaoImpl implements WalletDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean creditWallet(double amount, final String customerId) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Criteria cr = session.createCriteria(Wallet.class);
cr.add(Restrictions.eq("customerId", customerId));
Wallet walletInfo = (Wallet) cr.uniqueResult();
tx.commit();
final double newbalaance = walletInfo.getAmount() + amount;
walletInfo.setAmount(newbalaance);
Transaction tx2 = session.beginTransaction();
session.update(walletInfo);
tx2.commit();
session.close();
return true;
}
}
提前致谢。
最好的办法是 Spring 交易管理;您应该使用 spring 的 @Transactional 注释并配置 spring 以便启用事务注释
所以,回到你的代码...我会做这样的事情:
@Repository
public class WalletDaoImpl implements WalletDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean creditWallet(double amount, final String customerId) throws Exception
{
Session session = this.sessionFactory.getCurrentSession();
Criteria cr = session.createCriteria(Wallet.class);
cr.add(Restrictions.eq("customerId", customerId));
Wallet walletInfo = (Wallet) cr.uniqueResult();
final double newbalaance = walletInfo.getAmount() + amount;
walletInfo.setAmount(newbalaance);
session.update(walletInfo);
return true;
}
}
@Service
public class WalletServiceImpl implements WalletService
{
@Autowired
private WalletDao dao;
@Override
@Transactional
public boolean updateWallet(double amount, final String customerId) throws Exception
{
return dao.creditWallet(amount, customerId);
}
}
这里有更深入的解释:Spring transaction managment