@Override public <T> T findUniqueByNamedProperties(Class<T> entityClass, Map<String, ?> params) { Validate.notNull(entityClass); Validate.notEmpty(params); List<T> resultList = findByNamedProperties(entityClass, params); Validate.isTrue(resultList.size() < 2, "Found more than one persistent instance of type " + StringUtils.unqualify(entityClass.getName() + " with parameters " + params.toString())); return resultList.size() == 1 ? resultList.get(0) : null; }
@Override public <T> List<T> findAll(Class<T> entityClass) { Validate.notNull(entityClass); return query("select type from " + StringUtils.unqualify(entityClass.getName()) + " type"); }
/** * {@inheritDoc} * <p/> * This method overwrites the base class method by first updating an entity's audit fields if the entity is of type AuditableEntity. */ @Override public <T> T save(T entity) { updateAuditFields(entity); return super.save(entity); }
@Override public <T> List<T> queryByNamedParams(String queryString, Map<String, ?> params) { Validate.notEmpty(queryString); Validate.notEmpty(params); return executeQueryWithNamedParams(entityManager.createQuery(queryString), params); }
@Test public void testGetPredicateForInClauseOneChunk() { // Create the JPA builder, query, and entity root. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class); // Get the predicate for the "in" clause with 1000 values. Predicate predicate = baseJpaDaoImpl .getPredicateForInClause(builder, businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue), getPartitionValueList(1000)); // We expect to get back an "in" predicate with a single chunk of 1000 partition values. assertTrue(predicate instanceof InPredicate); assertEquals(1000, ((InPredicate) predicate).getValues().size()); }
@Override public <T> List<T> findByNamedProperties(Class<T> entityClass, Map<String, ?> params) { Validate.notNull(entityClass); Validate.notEmpty(params); StringBuilder queryStringBuilder = new StringBuilder("select type from " + StringUtils.unqualify(entityClass.getName()) + " type where "); Iterator<String> iterator = params.keySet().iterator(); int index = 0; while (iterator.hasNext()) { String propertyName = iterator.next(); if (index > 0) { queryStringBuilder.append(" and "); } queryStringBuilder.append("(type.").append(propertyName).append(" = :").append(propertyName).append(')'); index++; } return queryByNamedParams(queryStringBuilder.toString(), params); }
@Override public <T> List<T> queryByNamedParams(String queryString, Map<String, ?> params) { Validate.notEmpty(queryString); Validate.notEmpty(params); return executeQueryWithNamedParams(entityManager.createQuery(queryString), params); }
@Test public void testGetPredicateForInClauseTwoChunks() { // Create the JPA builder, query, and entity root. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class); // Get the predicate for the "in" clause with 1001 values (1 greater than the default chunking size). Predicate predicate = baseJpaDaoImpl .getPredicateForInClause(builder, businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue), getPartitionValueList(1001)); // We expect to get back an "or" of 2 "in" expressions where each "in" expression is a list of chunked partition values. assertEquals(Predicate.BooleanOperator.OR, predicate.getOperator()); List<Expression<Boolean>> expressions = predicate.getExpressions(); assertEquals(2, expressions.size()); // The first "in" clause will have the first 1000 elements and the second "in" clause will have the extra "1" element. assertEquals(1000, ((InPredicate) expressions.get(0)).getValues().size()); assertEquals(1, ((InPredicate) expressions.get(1)).getValues().size()); }
@Override public <T> List<T> findByNamedProperties(Class<T> entityClass, Map<String, ?> params) { Validate.notNull(entityClass); Validate.notEmpty(params); StringBuilder queryStringBuilder = new StringBuilder("select type from " + StringUtils.unqualify(entityClass.getName()) + " type where "); Iterator<String> iterator = params.keySet().iterator(); int index = 0; while (iterator.hasNext()) { String propertyName = iterator.next(); if (index > 0) { queryStringBuilder.append(" and "); } queryStringBuilder.append("(type.").append(propertyName).append(" = :").append(propertyName).append(')'); index++; } return queryByNamedParams(queryStringBuilder.toString(), params); }
/** * {@inheritDoc} * <p/> * This method overwrites the base class method by first updating an entity's audit fields if the entity is of type AuditableEntity. */ @Override public <T> T save(T entity) { updateAuditFields(entity); return super.save(entity); }
@Override public <T> List<T> findAll(Class<T> entityClass) { Validate.notNull(entityClass); return query("select type from " + StringUtils.unqualify(entityClass.getName()) + " type"); }
@Override public <T> T findUniqueByNamedProperties(Class<T> entityClass, Map<String, ?> params) { Validate.notNull(entityClass); Validate.notEmpty(params); List<T> resultList = findByNamedProperties(entityClass, params); Validate.isTrue(resultList.size() < 2, "Found more than one persistent instance of type " + StringUtils.unqualify(entityClass.getName() + " with parameters " + params.toString())); return resultList.size() == 1 ? resultList.get(0) : null; }
@Override public <T> T saveAndRefresh(T entity) { // Save the entity. save(entity); // Flush (i.e. persist) the entity and re-load it to retrieve the create/update date that was populated by the database. entityManager.flush(); entityManager.refresh(entity); // Return the persisted entity. return entity; }
@Override public <T> T saveAndRefresh(T entity) { // Save the entity. save(entity); // Flush (i.e. persist) the entity and re-load it to retrieve the create/update date that was populated by the database. entityManager.flush(); entityManager.refresh(entity); // Return the persisted entity. return entity; }