private static <T> T withoutFlush(EntityManager entityManager, boolean flushAtEnd, Function<EntityManager, T> task) { FlushModeType fm = entityManager.getFlushMode(); entityManager.setFlushMode(FlushModeType.COMMIT); try { T result = task.apply(entityManager); entityManager.setFlushMode(fm); if (flushAtEnd) { entityManager.flush(); } return result; } catch (RuntimeException e) { entityManager.setFlushMode(fm); throw e; } }
protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters, List<Order> orders) { Assert.notNull(criteriaQuery); Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Root<T> root = getRoot(criteriaQuery); addRestrictions(criteriaQuery, filters); addOrders(criteriaQuery, orders); if (criteriaQuery.getOrderList().isEmpty()) { if (OrderEntity.class.isAssignableFrom(entityClass)) { criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME))); } else { criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME))); } } TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (first != null) { query.setFirstResult(first); } if (count != null) { query.setMaxResults(count); } return query.getResultList(); }
public BigDecimal getSalesAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal> get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date> get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date> get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
public Integer getSalesVolume(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer> get("shippedQuantity"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date> get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date> get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
/** * 清除商品属性值并删除 * * @param productCategory * 商品分类 */ @Override public void remove(ProductCategory productCategory) { if (productCategory != null) { StringBuffer jpql = new StringBuffer("update Product product set "); for (int i = 0; i < Product.ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) { String propertyName = Product.ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i; if (i == 0) { jpql.append("product." + propertyName + " = null"); } else { jpql.append(", product." + propertyName + " = null"); } } jpql.append(" where product.productCategory = :productCategory"); entityManager.createQuery(jpql.toString()).setFlushMode(FlushModeType.COMMIT).setParameter("productCategory", productCategory).executeUpdate(); super.remove(productCategory); } }
/** * 处理商品参数并删除 * * @param parameterGroup * 参数组 * @return 参数组 */ @Override public void remove(ParameterGroup parameterGroup) { if (parameterGroup != null) { for (int i = 0; i < parameterGroup.getParameters().size(); i++) { Parameter parameter = parameterGroup.getParameters().get(i); String jpql = "select product from Product product join product.parameterValue parameterValue where index(parameterValue) = :parameter"; List<Product> products = entityManager.createQuery(jpql, Product.class).setFlushMode(FlushModeType.COMMIT).setParameter("parameter", parameter).getResultList(); for (Product product : products) { product.getParameterValue().remove(parameter); if (i % 20 == 0) { super.flush(); super.clear(); } } } super.remove(super.merge(parameterGroup)); } }
/** * 忽略默认、清除会员价并删除 * * @param memberRank * 会员等级 */ @Override public void remove(MemberRank memberRank) { if (memberRank != null && !memberRank.getIsDefault()) { String jpql = "select product from Product product join product.memberPrice memberPrice where index(memberPrice) = :memberRank"; List<Product> products = entityManager.createQuery(jpql, Product.class).setFlushMode(FlushModeType.COMMIT).setParameter("memberRank", memberRank).getResultList(); for (int i = 0; i < products.size(); i++) { Product product = products.get(i); product.getMemberPrice().remove(memberRank); if (i % 20 == 0) { super.flush(); super.clear(); } } super.remove(super.merge(memberRank)); } }
public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Member> member = criteriaQuery.from(Member.class); Join<Product, Order> orders = member.join("orders"); criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance"), criteriaBuilder.sum(orders.<BigDecimal> get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(orders.<Date> get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(orders.<Date> get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal> get("amountPaid")))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, com.easyshopping.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer> get("quantity")), criteriaBuilder.sum(criteriaBuilder.prod(orderItems.<Integer> get("quantity"), orderItems.<BigDecimal> get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date> get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date> get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(criteriaBuilder.prod(orderItems.<Integer> get("quantity"), orderItems.<BigDecimal> get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
@Test @Ignore("TODO replace persistence provider") public void aliasRules() { TestSecurityContext.authenticate(USER); EntityManagerFactory factory = Persistence.createEntityManagerFactory("alias"); EntityManager entityManager = factory.createEntityManager(); EntityManager mock = (EntityManager)entityManager.getDelegate(); reset(mock); String originalQuery = "SELECT c.text, SUM(c.id) AS cSum FROM Contact AS c " + "WHERE c.owner.name = :name GROUP BY c.text ORDER BY cSum"; // TODO check why the rule is applied twice String filteredQuery = " SELECT c.text, SUM(c.id) AS cSum FROM Contact c " + "WHERE (c.owner.name = :name) AND (c.owner.name = 'user') " + "GROUP BY c.text ORDER BY cSum"; when(mock.isOpen()).thenReturn(true); when(mock.getFlushMode()).thenReturn(FlushModeType.AUTO); when(mock.createQuery(filteredQuery)).thenReturn(mock(Query.class)); entityManager.createQuery(originalQuery); verify(mock).createQuery(filteredQuery); }
@Override public List<Product> findByGoodsWithExcludes(Goods goods, Set<Product> excludes) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class); Root<Product> root = criteriaQuery.from(Product.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (goods != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("goods"), goods)); } if (excludes != null && !excludes.isEmpty()) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList(); }
@BeforeClass public void setUpClass() throws Exception { entityManagerFactory = Persistence .createEntityManagerFactory("postgres_test"); entityManager = entityManagerFactory.createEntityManager(); entityManager.setFlushMode(FlushModeType.COMMIT); entityManager.clear(); tx = entityManager.getTransaction(); // provision daos dao = new EntryEntityDao(); dao.setEntityManager(entityManager); mapStore = new HazelcastMapStore(String.class, User.class, dao); store = new MyHazelcastInstance(mapStore, Constants.POSTGRES_MAP_STORE); worker = new HazelcastWorker(Constants.POSTGRES_MAP_STORE); }
/** * If the description list of a source element was changed, * this ensures that the description(s) of its associated LinkElements is up-to-date. */ public void updateAllFormElementsWithDescriptionChanged(FormElement formElement) { Iterator<Description> newDescriptionIterator = formElement.getDescriptionList().iterator(); while ( newDescriptionIterator.hasNext() ) { Description newDescription = newDescriptionIterator.next(); if ( ! newDescription.isNew() ) { Query query = em.createNativeQuery("update form_element set description = :desc from description d where form_element.link_id=:uuid and form_element.description = d.source_description_text and d.id = :id"); query.setParameter("desc", newDescription.getDescription() ); query.setParameter("uuid", formElement.isLink() && !formElement.isExternalQuestion() ? (( LinkElement )formElement).getSourceId() : formElement.getUuid() ); query.setParameter("id", newDescription.getId() ); query.setFlushMode(FlushModeType.COMMIT); query.executeUpdate(); } } }
@Test public void shouldNotSaveEntityWithCommitFlush() { Plane boeing = new Plane(6L, "boeing"); doInTransaction(() -> { em.persist(boeing); em.setFlushMode(FlushModeType.COMMIT); em.createQuery("select p from Plane p").getResultList(); em.clear(); }); doInTransaction(() -> assertThat(em.find(Plane.class, 6L)).isNull()); }
@SuppressWarnings({ "unused", "unchecked" }) public void testQueryNoPersonsShared() { EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory); Query q = em.createQuery("select p from Person as p"); q.setFlushMode(FlushModeType.AUTO); List<Person> people = q.getResultList(); try { assertNull(q.getSingleResult()); fail("Should have thrown NoResultException"); } catch (NoResultException ex) { // expected } }
public List<FriendLink> findList(Type type) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<FriendLink> criteriaQuery = criteriaBuilder.createQuery(FriendLink.class); Root<FriendLink> root = criteriaQuery.from(FriendLink.class); criteriaQuery.select(root); if (type != null) { criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type)); } criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order"))); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList(); }
public DeliveryTemplate findDefault() { try { String jpql = "select deliveryTemplate from DeliveryTemplate deliveryTemplate where deliveryTemplate.isDefault = true"; return entityManager.createQuery(jpql, DeliveryTemplate.class).setFlushMode(FlushModeType.COMMIT).getSingleResult(); } catch (NoResultException e) { return null; } }
/** * 处理默认并保存 * * @param deliveryTemplate * 快递单模板 */ @Override public void persist(DeliveryTemplate deliveryTemplate) { Assert.notNull(deliveryTemplate); if (deliveryTemplate.getIsDefault()) { String jpql = "update DeliveryTemplate deliveryTemplate set deliveryTemplate.isDefault = false where deliveryTemplate.isDefault = true"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).executeUpdate(); } super.persist(deliveryTemplate); }
/** * 处理默认并更新 * * @param deliveryTemplate * 快递单模板 * @return 快递单模板 */ @Override public DeliveryTemplate merge(DeliveryTemplate deliveryTemplate) { Assert.notNull(deliveryTemplate); if (deliveryTemplate.getIsDefault()) { String jpql = "update DeliveryTemplate deliveryTemplate set deliveryTemplate.isDefault = false where deliveryTemplate.isDefault = true and deliveryTemplate != :deliveryTemplate"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("deliveryTemplate", deliveryTemplate).executeUpdate(); } return super.merge(deliveryTemplate); }
private long getLastValue(Type type) { String jpql = "select sn from Sn sn where sn.type = :type"; Sn sn = entityManager.createQuery(jpql, Sn.class).setFlushMode(FlushModeType.COMMIT).setLockMode(LockModeType.PESSIMISTIC_WRITE).setParameter("type", type).getSingleResult(); long lastValue = sn.getLastValue(); sn.setLastValue(lastValue + 1); entityManager.merge(sn); return lastValue; }
public boolean codeExists(String code) { if (code == null) { return false; } String jpql = "select count(*) from CouponCode couponCode where lower(couponCode.code) = lower(:code)"; Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("code", code).getSingleResult(); return count > 0; }
public CouponCode findByCode(String code) { if (code == null) { return null; } try { String jpql = "select couponCode from CouponCode couponCode where lower(couponCode.code) = lower(:code)"; return entityManager.createQuery(jpql, CouponCode.class).setFlushMode(FlushModeType.COMMIT).setParameter("code", code).getSingleResult(); } catch (NoResultException e) { return null; } }
public Shipping findBySn(String sn) { if (sn == null) { return null; } String jpql = "select shipping from Shipping shipping where lower(shipping.sn) = lower(:sn)"; try { return entityManager.createQuery(jpql, Shipping.class).setFlushMode(FlushModeType.COMMIT).setParameter("sn", sn).getSingleResult(); } catch (NoResultException e) { return null; } }
public List<Tag> findList(Type type) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tag> criteriaQuery = criteriaBuilder.createQuery(Tag.class); Root<Tag> root = criteriaQuery.from(Tag.class); criteriaQuery.select(root); if (type != null) { criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type)); } criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order"))); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList(); }
/** * 保存并处理默认 * * @param receiver * 收货地址 */ @Override public void persist(Receiver receiver) { Assert.notNull(receiver); Assert.notNull(receiver.getMember()); if (receiver.getIsDefault()) { String jpql = "update Receiver receiver set receiver.isDefault = false where receiver.member = :member and receiver.isDefault = true"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("member", receiver.getMember()).executeUpdate(); } super.persist(receiver); }
/** * 更新并处理默认 * * @param receiver * 收货地址 * @return 收货地址 */ @Override public Receiver merge(Receiver receiver) { Assert.notNull(receiver); Assert.notNull(receiver.getMember()); if (receiver.getIsDefault()) { String jpql = "update Receiver receiver set receiver.isDefault = false where receiver.member = :member and receiver.isDefault = true and receiver != :receiver"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("member", receiver.getMember()).setParameter("receiver", receiver).executeUpdate(); } return super.merge(receiver); }
public boolean isReviewed(Member member, Product product) { if (member == null || product == null) { return false; } String jqpl = "select count(*) from Review review where review.member = :member and review.product = :product"; Long count = entityManager.createQuery(jqpl, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("member", member).setParameter("product", product).getSingleResult(); return count > 0; }
public long calculateTotalScore(Product product) { if (product == null) { return 0L; } String jpql = "select sum(review.score) from Review review where review.product = :product and review.isShow = :isShow"; Long totalScore = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("product", product).setParameter("isShow", true).getSingleResult(); return totalScore != null ? totalScore : 0L; }
public long calculateScoreCount(Product product) { if (product == null) { return 0L; } String jpql = "select count(*) from Review review where review.product = :product and review.isShow = :isShow"; return entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("product", product).setParameter("isShow", true).getSingleResult(); }
protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) { Assert.notNull(criteriaQuery); Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); if (pageable == null) { pageable = new Pageable(); } CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Root<T> root = getRoot(criteriaQuery); addRestrictions(criteriaQuery, pageable); addOrders(criteriaQuery, pageable); if (criteriaQuery.getOrderList().isEmpty()) { if (OrderEntity.class.isAssignableFrom(entityClass)) { criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME))); } else { criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME))); } } long total = count(criteriaQuery, null); int totalPages = (int) Math.ceil((double) total / (double) pageable.getPageSize()); if (totalPages < pageable.getPageNumber()) { pageable.setPageNumber(totalPages); } TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); query.setMaxResults(pageable.getPageSize()); return new Page<T>(query.getResultList(), total, pageable); }
protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { Assert.notNull(criteriaQuery); Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); addRestrictions(criteriaQuery, filters); CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class); for (Root<?> root : criteriaQuery.getRoots()) { Root<?> dest = countCriteriaQuery.from(root.getJavaType()); dest.alias(getAlias(root)); copyJoins(root, dest); } Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType()); countCriteriaQuery.select(criteriaBuilder.count(countRoot)); if (criteriaQuery.getGroupList() != null) { countCriteriaQuery.groupBy(criteriaQuery.getGroupList()); } if (criteriaQuery.getGroupRestriction() != null) { countCriteriaQuery.having(criteriaQuery.getGroupRestriction()); } if (criteriaQuery.getRestriction() != null) { countCriteriaQuery.where(criteriaQuery.getRestriction()); } return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
public Order findBySn(String sn) { if (sn == null) { return null; } String jpql = "select orders from Order orders where lower(orders.sn) = lower(:sn)"; try { return entityManager.createQuery(jpql, Order.class).setFlushMode(FlushModeType.COMMIT).setParameter("sn", sn).getSingleResult(); } catch (NoResultException e) { return null; } }
public List<Area> findRoots(Integer count) { String jpql = "select area from Area area where area.parent is null order by area.order asc"; TypedQuery<Area> query = entityManager.createQuery(jpql, Area.class).setFlushMode(FlushModeType.COMMIT); if (count != null) { query.setMaxResults(count); } return query.getResultList(); }
public DeliveryCenter findDefault() { try { String jpql = "select deliveryCenter from DeliveryCenter deliveryCenter where deliveryCenter.isDefault = true"; return entityManager.createQuery(jpql, DeliveryCenter.class).setFlushMode(FlushModeType.COMMIT).getSingleResult(); } catch (NoResultException e) { return null; } }
/** * 处理默认并保存 * * @param deliveryCenter * 发货点 */ @Override public void persist(DeliveryCenter deliveryCenter) { Assert.notNull(deliveryCenter); if (deliveryCenter.getIsDefault()) { String jpql = "update DeliveryCenter deliveryCenter set deliveryCenter.isDefault = false where deliveryCenter.isDefault = true"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).executeUpdate(); } super.persist(deliveryCenter); }
/** * 处理默认并更新 * * @param deliveryCenter * 发货点 * @return 发货点 */ @Override public DeliveryCenter merge(DeliveryCenter deliveryCenter) { Assert.notNull(deliveryCenter); if (deliveryCenter.getIsDefault()) { String jpql = "update DeliveryCenter deliveryCenter set deliveryCenter.isDefault = false where deliveryCenter.isDefault = true and deliveryCenter != :deliveryCenter"; entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("deliveryCenter", deliveryCenter).executeUpdate(); } return super.merge(deliveryCenter); }
public boolean pluginIdExists(String pluginId) { if (pluginId == null) { return false; } String jpql = "select count(*) from PluginConfig pluginConfig where pluginConfig.pluginId = :pluginId"; Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("pluginId", pluginId).getSingleResult(); return count > 0; }
public PluginConfig findByPluginId(String pluginId) { if (pluginId == null) { return null; } try { String jpql = "select pluginConfig from PluginConfig pluginConfig where pluginConfig.pluginId = :pluginId"; return entityManager.createQuery(jpql, PluginConfig.class).setFlushMode(FlushModeType.COMMIT).setParameter("pluginId", pluginId).getSingleResult(); } catch (NoResultException e) { return null; } }
public List<ProductCategory> findRoots(Integer count) { String jpql = "select productCategory from ProductCategory productCategory where productCategory.parent is null order by productCategory.order asc"; TypedQuery<ProductCategory> query = entityManager.createQuery(jpql, ProductCategory.class).setFlushMode(FlushModeType.COMMIT); if (count != null) { query.setMaxResults(count); } return query.getResultList(); }
public List<ProductCategory> findParents(ProductCategory productCategory, Integer count) { if (productCategory == null || productCategory.getParent() == null) { return Collections.<ProductCategory> emptyList(); } String jpql = "select productCategory from ProductCategory productCategory where productCategory.id in (:ids) order by productCategory.grade asc"; TypedQuery<ProductCategory> query = entityManager.createQuery(jpql, ProductCategory.class).setFlushMode(FlushModeType.COMMIT).setParameter("ids", productCategory.getTreePaths()); if (count != null) { query.setMaxResults(count); } return query.getResultList(); }