MyTetra Share
Делитесь знаниями!
EntityManager
Время создания: 02.12.2019 14:34
Раздел: INFO - Development - CUBA
Запись: wwwlir/Tetra/master/base/1575268457pndzxnmgei/text.html на raw.githubusercontent.com

Пример использования EntityManager в сервисе:

@Service(SalesService.NAME)

public class SalesServiceBean implements SalesService {


@Inject

private Persistence persistence;


@Override

public BigDecimal calculateSales(UUID customerId) {

BigDecimal result;

// start transaction

try (Transaction tx = persistence.createTransaction()) {

// get EntityManager for the current transaction

EntityManager em = persistence.getEntityManager();

// create and execute Query

Query query = em.createQuery(

"select sum(o.amount) from sample$Order o where o.customer.id = :customerId");

query.setParameter("customerId", customerId);

result = (BigDecimal) query.getFirstResult();

// commit transaction

tx.commit();

}

return result != null ? result : BigDecimal.ZERO;

}

}

Частичные сущности


Persistence persistence = AppBeans.get(Persistence.class);
BigDecimal result = null;
try
(Transaction tx = persistence.createTransaction()) {
// get EntityManager for the current transaction
EntityManager em = persistence.getEntityManager();
// create and execute Query
Query query = em.createQuery("select count(p) from rtneo$Payment p where p.inn = :inn");
query.setParameter("inn", contract.getContragent().getInn());
result = (BigDecimal) query.getFirstResult();
// commit transaction
tx.commit();
}
return result != null ? result : BigDecimal.ZERO;

 
MyTetra Share v.0.65
Яндекс индекс цитирования