@SuppressWarnings("unchecked") @Override public <X> BasicUserType<X> getBasicUserType(Class<X> clazz) { BasicUserType<?> userType = basicUserTypes.get(clazz); if (userType == null) { if (clazz.isEnum()) { // Enums are always considered immutable userType = ImmutableBasicUserType.INSTANCE; } else if (entityMetamodel.getEntity(clazz) != null) { userType = entityBasicUserType; } else if (Date.class.isAssignableFrom(clazz)) { userType = DateBasicUserType.INSTANCE; } else if (Calendar.class.isAssignableFrom(clazz)) { userType = CalendarBasicUserType.INSTANCE; } else { userType = MutableBasicUserType.INSTANCE; } } return (BasicUserType<X>) userType; } }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
@Override public void visit(TreatExpression expression) { EntityType<?> type = metamodel.getEntity(expression.getType()); currentPosition.setAttribute(null); currentPosition.setCurrentType(type); }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
@Override protected void validate(RepositoryMetadata repositoryMetadata) { super.validate(repositoryMetadata); if (cbf.getService(EntityMetamodel.class).getEntity(repositoryMetadata.getDomainType()) == null) { throw new InvalidDataAccessApiUsageException( String.format("Cannot implement repository %s when using a non-entity domain type %s. Only types annotated with @Entity are supported!", repositoryMetadata.getRepositoryInterface().getName(), repositoryMetadata.getDomainType().getName())); } }
private static boolean shouldPassThrough(EntityViewManagerImpl evm, ManagedViewType<?> viewType, AbstractMethodAttribute<?, ?> attribute) { // For an attribute being eligible for pass through, the declaring view must be for an embeddable type // and the attribute must be update mappable return evm.getMetamodel().getEntityMetamodel().getEntity(viewType.getEntityClass()) == null && attribute.isUpdateMappable(); }
@Override public void visit(TreatExpression expression) { boolean handled = false; if (expression.getExpression() instanceof PathExpression) { PathExpression treatPath = (PathExpression) expression.getExpression(); if (treatPath.getExpressions().size() == 1 && skipBaseNodeAlias.equals(treatPath.getExpressions().get(0).toString())) { // When we encounter a naked root treat like "TREAT(alias AS Subtype)" we always skip it handled = true; } } if (!handled) { expression.getExpression().accept(this); } EntityType<?> type = metamodel.getEntity(expression.getType()); // TODO: should we check if the type is actually a sub- or super type? currentPosition.setCurrentType(type); currentPosition.setValueType(type); }
@Override public void visit(TreatExpression expression) { boolean handled = false; if (expression.getExpression() instanceof PathExpression) { PathExpression treatPath = (PathExpression) expression.getExpression(); if (treatPath.getExpressions().size() == 1 && skipBaseNodeAlias.equals(treatPath.getExpressions().get(0).toString())) { // When we encounter a naked root treat like "TREAT(alias AS Subtype)" we always skip it handled = true; } } if (!handled) { expression.getExpression().accept(this); } EntityType<?> type = metamodel.getEntity(expression.getType()); // TODO: should we check if the type is actually a sub- or super type? currentPosition.setCurrentType(type); currentPosition.setValueType(type); }
public AbstractViewToEntityMapper(String attributeLocation, EntityViewManagerImpl evm, Class<?> viewTypeClass, Set<Type<?>> readOnlyAllowedSubtypes, Set<Type<?>> persistAllowedSubtypes, Set<Type<?>> updateAllowedSubtypes, EntityLoader entityLoader, AttributeAccessor viewIdAccessor, boolean persistAllowed, EntityViewUpdaterImpl owner, String ownerMapping) { this.attributeLocation = attributeLocation; ManagedViewTypeImplementor<?> managedViewTypeImplementor = evm.getMetamodel().managedView(viewTypeClass); this.isEmbeddable = evm.getMetamodel().getEntityMetamodel().getEntity(managedViewTypeImplementor.getEntityClass()) == null; Map<Class<?>, EntityViewUpdater> persistUpdater = new HashMap<>(); Map<Class<?>, EntityViewUpdater> updateUpdater = new HashMap<>();
private String createQueryString(EntityViewManagerImpl evm, ManagedViewType<?> subviewType) { EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel(); EntityType<?> entityType = entityMetamodel.getEntity(entityClass); // We can't query embeddables if (entityType == null) { return null; } Set<String> fetchJoinableRelations = new HashSet<>(subviewType.getAttributes().size()); addFetchJoinableRelations(fetchJoinableRelations, "", subviewType); CriteriaBuilderFactory cbf = evm.getCriteriaBuilderFactory(); EntityManagerFactory emf = cbf.getService(EntityManagerFactory.class); EntityManager em = emf.createEntityManager(); try { if (fetchJoinableRelations.isEmpty()) { return null; } else { return cbf.create(em, entityClass) .fetch(fetchJoinableRelations.toArray(new String[fetchJoinableRelations.size()])) .where(JpaMetamodelUtils.getSingleIdAttribute(entityType).getName()).eqExpression(":id") .getQueryString(); } } finally { em.close(); } }
public ObjectBuilder<?> createObjectBuilder(ManagedViewTypeImplementor<?> viewType, MappingConstructorImpl<?> mappingConstructor, String viewName, Class<?> rootType, String entityViewRoot, String embeddingViewPath, FullQueryBuilder<?, ?> criteriaBuilder, EntityViewConfiguration configuration, int offset, int suffix) { ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class); if (!viewType.getEntityClass().isAssignableFrom(rootType)) { if (rootType.isAssignableFrom(viewType.getEntityClass())) { entityViewRoot = "TREAT(" + entityViewRoot + " AS " + metamodel.getEntityMetamodel().getEntity(viewType.getJavaType()).getName() + ")"; } else { throw new IllegalArgumentException("The given view type with the entity type '" + viewType.getEntityClass().getName() + "' can not be applied to the query builder with result type '" + rootType.getName() + "'"); } } MacroConfiguration originalMacroConfiguration = ef.getDefaultMacroConfiguration(); ExpressionFactory cachingExpressionFactory = ef.unwrap(AbstractCachingExpressionFactory.class); JpqlMacro viewRootJpqlMacro = new DefaultViewRootJpqlMacro(entityViewRoot); EmbeddingViewJpqlMacro embeddingViewJpqlMacro = configuration.getEmbeddingViewJpqlMacro(); Map<String, MacroFunction> macros = new HashMap<>(); macros.put("view_root", new JpqlMacroAdapter(viewRootJpqlMacro, cachingExpressionFactory)); macros.put("embedding_view", new JpqlMacroAdapter(embeddingViewJpqlMacro, cachingExpressionFactory)); MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros); ef = new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration); criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro); return getTemplate(ef, viewType, mappingConstructor, viewName, entityViewRoot, embeddingViewPath, embeddingViewJpqlMacro, offset) .createObjectBuilder(criteriaBuilder, configuration.getOptionalParameters(), configuration, suffix); }
public Map<String, String> determineWritableMappedByMappings(ManagedType<?> managedType, String mappedBy, MetamodelBuildingContext context) { ViewMapping elementViewMapping = getElementViewMapping(); EntityType<?> elementType; if (elementViewMapping != null) { elementType = context.getEntityMetamodel().getEntity(elementViewMapping.getEntityClass()); } else { Class<?> declaredElementType = getDeclaredElementType(); if (declaredElementType != null) { elementType = context.getEntityMetamodel().getEntity(declaredElementType); } else { elementType = context.getEntityMetamodel().getEntity(getDeclaredType()); } } if (elementType == null) { return null; } EntityType<?> entityType = (EntityType<?>) managedType; try { Map<String, String> writableMappedByMappings = context.getJpaProvider().getWritableMappedByMappings(entityType, elementType, mappedBy, ((Mapping) mapping).value()); if (writableMappedByMappings == null) { return null; } else { return Collections.unmodifiableMap(writableMappedByMappings); } } catch (RuntimeException ex) { // Graceful error message for cases when an inverse attribute was mapped on a wrong type context.addError("Couldn't determine writable mappings for the mapped by mapping '" + mappedBy + "' on the entity '" + elementType.getName() + "' declared by the entity '" + entityType.getName() + "' through a entity view mapping at the " + getErrorLocation()); return null; } }
public AbstractUnmappedAttributeCascadeDeleter(EntityViewManagerImpl evm, String attributeName, ExtendedAttribute<?, ?> attribute) { EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel(); this.elementEntityClass = attribute.getElementClass(); this.attributeName = attributeName; if (entityMetamodel.getEntity(elementEntityClass) == null) { this.elementIdAttributeName = null; this.attributeValuePath = attributeName; this.cascadeDeleteElement = false; } else { ExtendedManagedType extendedManagedType = entityMetamodel.getManagedType(ExtendedManagedType.class, elementEntityClass); this.elementIdAttributeName = extendedManagedType.getIdAttribute().getName(); this.attributeValuePath = attributeName + "." + elementIdAttributeName; this.cascadeDeleteElement = attribute.isDeleteCascaded(); } }
EntityType<?> entityType = m.getEntity(rootNode.getJavaType()); if (entityType == null) {
} else { subAttributes = new HashSet<>(); EntityType<?> entity = evm.getMetamodel().getEntityMetamodel().getEntity(entityClass); for (String propertyName : evm.getJpaProvider().getIdentifierOrUniqueKeyEmbeddedPropertyNames(entity, mappingPrefix + attribute.getName())) { subAttributes.add(managedType.getAttribute(propertyName));
viewIdAccessor = Accessors.forViewId(evm, (ViewType<?>) viewType, true); if (evm.getMetamodel().getEntityMetamodel().getEntity(viewType.getEntityClass()) == null) { Class<?> viewTypeClass = viewType.getJavaType(); return new EmbeddableUpdaterBasedViewToEntityMapper(
if (evm.getMetamodel().getEntityMetamodel().getEntity(viewType.getEntityClass()) == null) { return new EmbeddableUpdaterBasedViewToEntityMapper( attributeLocation,