use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.
the class MethodAttributeMapping method determineMappedBy.
public String determineMappedBy(ManagedType<?> managedType, String mapping, MetamodelBuildingContext context, EmbeddableOwner embeddableMapping) {
String mappedBy;
if (embeddableMapping == null) {
mappedBy = this.mappedBy;
} else {
if (embeddableMappedByMap == null) {
embeddableMappedByMap = new HashMap<>(1);
}
mappedBy = embeddableMappedByMap.get(embeddableMapping);
}
if (mappedBy != null) {
if (mappedBy.isEmpty()) {
return null;
}
return mappedBy;
}
if (embeddableMapping == null) {
this.mappedBy = "";
} else {
embeddableMappedByMap.put(embeddableMapping, "");
}
if (mapping == null || mapping.isEmpty()) {
return null;
}
if (!(managedType instanceof EntityType<?>)) {
// Can't determine the inverse mapped by attribute of a non-entity
return null;
}
try {
AttributePath basicAttributePath = context.getJpaProvider().getJpaMetamodelAccessor().getAttributePath(context.getEntityMetamodel(), managedType, mapping);
List<Attribute<?, ?>> attributes = basicAttributePath.getAttributes();
for (int i = 1; i < attributes.size(); i++) {
if (attributes.get(i).getDeclaringType().getPersistenceType() != Type.PersistenceType.EMBEDDABLE) {
// If the mapping goes over a non-embeddable, we can't determine a mapped by attribute name
return null;
}
}
mappedBy = context.getJpaProvider().getMappedBy((EntityType<?>) managedType, mapping);
if (embeddableMapping == null) {
this.mappedBy = mappedBy;
} else {
embeddableMappedByMap.put(embeddableMapping, mappedBy);
}
return mappedBy;
} catch (IllegalArgumentException ex) {
// if the mapping is invalid, we skip the determination as the error will be analyzed further at a later stage
return null;
}
}
use of com.blazebit.persistence.spi.AttributePath 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.AttributePath 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.AttributePath 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();
}
use of com.blazebit.persistence.spi.AttributePath in project blaze-persistence by Blazebit.
the class Accessors method forEntityMapping.
public static AttributeAccessor forEntityMapping(EntityViewManagerImpl evm, Class<?> entityClass, String mapping) {
if (mapping == null || mapping.isEmpty()) {
return new NestedAttributeAccessor(Collections.<AttributeAccessor>emptyList());
}
EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel();
AttributePath path = evm.getJpaProvider().getJpaMetamodelAccessor().getBasicAttributePath(entityMetamodel, entityMetamodel.managedType(entityClass), mapping);
List<Attribute<?, ?>> attributes = path.getAttributes();
if (attributes.size() == 1) {
return forEntityAttribute(evm, entityClass, attributes.get(0), null);
}
List<AttributeAccessor> mappers = new ArrayList<>(attributes.size());
Class<?> targetClass = entityClass;
for (int i = 0; i < attributes.size() - 1; i++) {
javax.persistence.metamodel.Attribute<?, ?> attribute = attributes.get(i);
Class<?> attributeClass = JpaMetamodelUtils.resolveFieldClass(targetClass, attribute);
mappers.add(forEntityAttribute(evm, targetClass, attribute, attributeClass));
targetClass = attributeClass;
}
mappers.add(forEntityAttribute(evm, targetClass, attributes.get(attributes.size() - 1), null));
return new NestedAttributeAccessor(mappers);
}
Aggregations