use of com.blazebit.persistence.spi.JpaMetamodelAccessor in project blaze-persistence by Blazebit.
the class BaseUpdateCriteriaBuilderImpl method addAttribute.
protected void addAttribute(String attributeName) {
// Just do that to assert the attribute exists
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attributeName);
Integer attributeBindIndex = setAttributeBindingMap.get(attributeName);
if (attributeBindIndex != null) {
throw new IllegalArgumentException("The attribute [" + attributeName + "] has already been bound!");
}
setAttributeBindingMap.put(attributeName, selectManager.getSelectInfos().size());
}
use of com.blazebit.persistence.spi.JpaMetamodelAccessor in project blaze-persistence by Blazebit.
the class AbstractFullQueryBuilder method addAttributes.
private void addAttributes(String prefix, Set<SingularAttribute<?, ?>> attributes, List<ResolvedExpression> resolvedExpressions, StringBuilder sb, JoinNode rootNode) {
for (SingularAttribute<?, ?> attribute : attributes) {
String attributeName;
if (prefix.isEmpty()) {
attributeName = attribute.getName();
} else {
attributeName = prefix + attribute.getName();
}
if (attribute.getType() instanceof EmbeddableType<?>) {
Set<SingularAttribute<?, ?>> subAttributes = new TreeSet<>(ATTRIBUTE_NAME_COMPARATOR);
subAttributes.addAll(((EmbeddableType<?>) attribute.getType()).getSingularAttributes());
addAttributes(attributeName + ".", subAttributes, resolvedExpressions, sb, rootNode);
} else {
sb.setLength(0);
rootNode.appendDeReference(sb, attributeName, false);
PathExpression expression = (PathExpression) rootNode.createExpression(attributeName);
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
expression.setPathReference(new SimplePathReference(rootNode, attributeName, getMetamodel().type(jpaMetamodelAccessor.getAttributePath(getMetamodel(), rootNode.getManagedType(), attributeName).getAttributeClass())));
resolvedExpressions.add(new ResolvedExpression(sb.toString(), expression));
}
}
}
use of com.blazebit.persistence.spi.JpaMetamodelAccessor in project blaze-persistence by Blazebit.
the class AbstractModificationCriteriaBuilder method getWithReturningQuery.
@SuppressWarnings("unchecked")
public <Z> TypedQuery<ReturningResult<Z>> getWithReturningQuery(String attribute, Class<Z> type) {
if (attribute == null) {
throw new NullPointerException("attribute");
}
if (type == null) {
throw new NullPointerException("type");
}
if (attribute.isEmpty()) {
throw new IllegalArgumentException("Invalid empty attribute");
}
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
AttributePath attrPath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attribute);
if (!type.isAssignableFrom(attrPath.getAttributeClass())) {
throw new IllegalArgumentException("The given expected field type is not of the expected type: " + attrPath.getAttributeClass().getName());
}
List<List<Attribute<?, ?>>> attributes = new ArrayList<List<Attribute<?, ?>>>();
attributes.add(attrPath.getAttributes());
Query baseQuery = em.createQuery(getBaseQueryStringWithCheck(null, null));
TypedQuery<Object[]> exampleQuery = getExampleQuery(attributes);
String[] returningColumns = getReturningColumns(attributes);
return getExecuteWithReturningQuery(exampleQuery, baseQuery, returningColumns, null);
}
use of com.blazebit.persistence.spi.JpaMetamodelAccessor in project blaze-persistence by Blazebit.
the class AbstractModificationCriteriaBuilder method returning.
@SuppressWarnings("unchecked")
public X returning(String cteAttribute, String modificationQueryAttribute) {
if (cteAttribute == null) {
throw new NullPointerException("cteAttribute");
}
if (modificationQueryAttribute == null) {
throw new NullPointerException("modificationQueryAttribute");
}
if (cteAttribute.isEmpty()) {
throw new IllegalArgumentException("Invalid empty cteAttribute");
}
if (modificationQueryAttribute.isEmpty()) {
throw new IllegalArgumentException("Invalid empty modificationQueryAttribute");
}
ExtendedAttribute attributeEntry = attributeEntries.get(cteAttribute);
if (attributeEntry == null) {
if (cteType.getAttribute(cteAttribute) != null) {
throw new IllegalArgumentException("Can't bind the embeddable cte attribute [" + cteAttribute + "] directly! Please bind the respective sub attributes.");
}
throw new IllegalArgumentException("The cte attribute [" + cteAttribute + "] does not exist!");
}
Class<?> queryAttrType;
if (isReturningEntityAliasAllowed && modificationQueryAttribute.equals(entityAlias)) {
// Our little special case, since there would be no other way to refer to the id as the object type
queryAttrType = entityType.getJavaType();
Attribute<?, ?> idAttribute = JpaMetamodelUtils.getSingleIdAttribute(entityType);
modificationQueryAttribute = idAttribute.getName();
} else {
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
AttributePath queryAttributePath = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, modificationQueryAttribute);
queryAttrType = queryAttributePath.getAttributeClass();
}
Class<?> cteAttrType = attributeEntry.getElementClass();
// but if it already supports the returning clause, it can only also support returning all columns
if (!cteAttrType.isAssignableFrom(queryAttrType)) {
throw new IllegalArgumentException("The given cte attribute '" + cteAttribute + "' with the type '" + cteAttrType.getName() + "'" + " can not be assigned with a value of the type '" + queryAttrType.getName() + "' of the query attribute '" + modificationQueryAttribute + "'!");
}
if (returningAttributeBindingMap.put(cteAttribute, modificationQueryAttribute) != null) {
throw new IllegalArgumentException("The cte attribute [" + cteAttribute + "] has already been bound!");
}
for (String column : attributeEntry.getColumnNames()) {
if (columnBindingMap.put(column, cteAttribute) != null) {
throw new IllegalArgumentException("The cte column [" + column + "] has already been bound!");
}
}
return (X) this;
}
use of com.blazebit.persistence.spi.JpaMetamodelAccessor in project blaze-persistence by Blazebit.
the class BaseUpdateCriteriaBuilderImpl method getForeignKeyParticipatingQueries.
protected Collection<Query> getForeignKeyParticipatingQueries() {
Map<String, Query> map = null;
JpaMetamodelAccessor jpaMetamodelAccessor = mainQuery.jpaProvider.getJpaMetamodelAccessor();
for (String attributeName : setAttributeBindingMap.keySet()) {
AttributePath path = jpaMetamodelAccessor.getBasicAttributePath(getMetamodel(), entityType, attributeName);
for (Attribute<?, ?> attributePart : path.getAttributes()) {
if (attributePart instanceof SingularAttribute<?, ?>) {
SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attributePart;
if (map == null) {
map = new HashMap<>();
}
if (singularAttribute.getType() instanceof EntityType<?>) {
String entityName = ((EntityType<?>) singularAttribute.getType()).getName();
if (!map.containsKey(entityName)) {
map.put(entityName, em.createQuery("select e from " + entityName + " e"));
}
break;
}
}
}
}
return map == null ? Collections.<Query>emptyList() : map.values();
}
Aggregations