use of org.hibernate.engine.spi.FilterDefinition in project hibernate-orm by hibernate.
the class ModelBinder method bindBasicEntityValues.
private void bindBasicEntityValues(MappingDocument sourceDocument, AbstractEntitySourceImpl entitySource, PersistentClass entityDescriptor) {
entityDescriptor.setEntityName(entitySource.getEntityNamingSource().getEntityName());
entityDescriptor.setJpaEntityName(entitySource.getEntityNamingSource().getJpaEntityName());
entityDescriptor.setClassName(entitySource.getEntityNamingSource().getClassName());
entityDescriptor.setDiscriminatorValue(entitySource.getDiscriminatorMatchValue() != null ? entitySource.getDiscriminatorMatchValue() : entityDescriptor.getEntityName());
// NOTE : entitySource#isLazy already accounts for MappingDefaults#areEntitiesImplicitlyLazy
if (StringHelper.isNotEmpty(entitySource.getProxy())) {
final String qualifiedProxyName = sourceDocument.qualifyClassName(entitySource.getProxy());
entityDescriptor.setProxyInterfaceName(qualifiedProxyName);
entityDescriptor.setLazy(true);
} else if (entitySource.isLazy()) {
entityDescriptor.setProxyInterfaceName(entityDescriptor.getClassName());
entityDescriptor.setLazy(true);
} else {
entityDescriptor.setProxyInterfaceName(null);
entityDescriptor.setLazy(false);
}
entityDescriptor.setAbstract(entitySource.isAbstract());
sourceDocument.getMetadataCollector().addImport(entitySource.getEntityNamingSource().getEntityName(), entitySource.getEntityNamingSource().getEntityName());
if (sourceDocument.getMappingDefaults().isAutoImportEnabled() && entitySource.getEntityNamingSource().getEntityName().indexOf('.') > 0) {
sourceDocument.getMetadataCollector().addImport(StringHelper.unqualify(entitySource.getEntityNamingSource().getEntityName()), entitySource.getEntityNamingSource().getEntityName());
}
if (entitySource.getTuplizerClassMap() != null) {
if (entitySource.getTuplizerClassMap().size() > 1) {
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfMultipleEntityModeSupport();
}
for (Map.Entry<EntityMode, String> tuplizerEntry : entitySource.getTuplizerClassMap().entrySet()) {
entityDescriptor.addTuplizer(tuplizerEntry.getKey(), tuplizerEntry.getValue());
}
}
if (StringHelper.isNotEmpty(entitySource.getXmlNodeName())) {
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
}
entityDescriptor.setDynamicInsert(entitySource.isDynamicInsert());
entityDescriptor.setDynamicUpdate(entitySource.isDynamicUpdate());
entityDescriptor.setBatchSize(entitySource.getBatchSize());
entityDescriptor.setSelectBeforeUpdate(entitySource.isSelectBeforeUpdate());
if (StringHelper.isNotEmpty(entitySource.getCustomPersisterClassName())) {
try {
entityDescriptor.setEntityPersisterClass(sourceDocument.getBootstrapContext().getClassLoaderAccess().classForName(entitySource.getCustomPersisterClassName()));
} catch (ClassLoadingException e) {
throw new MappingException(String.format(Locale.ENGLISH, "Unable to load specified persister class : %s", entitySource.getCustomPersisterClassName()), e, sourceDocument.getOrigin());
}
}
bindCustomSql(sourceDocument, entitySource, entityDescriptor);
final JdbcEnvironment jdbcEnvironment = sourceDocument.getMetadataCollector().getDatabase().getJdbcEnvironment();
for (String tableName : entitySource.getSynchronizedTableNames()) {
final Identifier physicalTableName = sourceDocument.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(tableName), jdbcEnvironment);
entityDescriptor.addSynchronizedTable(physicalTableName.render(jdbcEnvironment.getDialect()));
}
for (FilterSource filterSource : entitySource.getFilterSources()) {
String condition = filterSource.getCondition();
if (condition == null) {
final FilterDefinition filterDefinition = sourceDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
if (filterDefinition != null) {
condition = filterDefinition.getDefaultFilterCondition();
}
}
entityDescriptor.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
}
for (JaxbHbmNamedQueryType namedQuery : entitySource.getNamedQueries()) {
NamedQueryBinder.processNamedQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
}
for (JaxbHbmNamedNativeQueryType namedQuery : entitySource.getNamedNativeQueries()) {
NamedQueryBinder.processNamedNativeQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
}
entityDescriptor.setMetaAttributes(entitySource.getToolingHintContext().getMetaAttributeMap());
}
use of org.hibernate.engine.spi.FilterDefinition in project hibernate-orm by hibernate.
the class FilterDefinitionBinder method processFilterDefinition.
/**
* Handling for a {@code <filter-def/>} declaration.
*
* @param context Access to information relative to the mapping document containing this binding
* @param jaxbFilterDefinitionMapping The {@code <filter-def/>} JAXB mapping
*/
@SuppressWarnings("unchecked")
public static void processFilterDefinition(HbmLocalMetadataBuildingContext context, JaxbHbmFilterDefinitionType jaxbFilterDefinitionMapping) {
Map<String, Type> parameterMap = null;
String condition = jaxbFilterDefinitionMapping.getCondition();
for (Serializable content : jaxbFilterDefinitionMapping.getContent()) {
if (String.class.isInstance(content)) {
final String contentString = content.toString().trim();
if (StringHelper.isNotEmpty(contentString)) {
if (condition != null) {
log.debugf("filter-def [name=%s, origin=%s] defined multiple conditions, accepting arbitrary one", jaxbFilterDefinitionMapping.getName(), context.getOrigin().toString());
}
}
} else {
final JaxbHbmFilterParameterType jaxbParameterMapping;
if (JaxbHbmFilterParameterType.class.isInstance(content)) {
jaxbParameterMapping = (JaxbHbmFilterParameterType) content;
} else if (JAXBElement.class.isInstance(content)) {
final JAXBElement<JaxbHbmFilterParameterType> jaxbElement = (JAXBElement<JaxbHbmFilterParameterType>) content;
jaxbParameterMapping = jaxbElement.getValue();
} else {
throw new MappingException("Unable to decipher filter-def content type [" + content.getClass().getName() + "]", context.getOrigin());
}
if (parameterMap == null) {
parameterMap = new HashMap<String, Type>();
}
parameterMap.put(jaxbParameterMapping.getParameterName(), context.getMetadataCollector().getTypeResolver().heuristicType(jaxbParameterMapping.getParameterValueTypeName()));
}
}
context.getMetadataCollector().addFilterDefinition(new FilterDefinition(jaxbFilterDefinitionMapping.getName(), condition, parameterMap));
log.debugf("Processed filter definition : %s", jaxbFilterDefinitionMapping.getName());
}
use of org.hibernate.engine.spi.FilterDefinition in project hibernate-orm by hibernate.
the class ModelBinder method bindCollectionMetadata.
private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
binding.setRole(source.getAttributeRole().getFullPath());
binding.setInverse(source.isInverse());
binding.setMutable(source.isMutable());
binding.setOptimisticLocked(source.isIncludedInOptimisticLocking());
if (source.getCustomPersisterClassName() != null) {
binding.setCollectionPersisterClass(mappingDocument.getBootstrapContext().getClassLoaderAccess().classForName(mappingDocument.qualifyClassName(source.getCustomPersisterClassName())));
}
applyCaching(mappingDocument, source.getCaching(), binding);
// bind the collection type info
String typeName = source.getTypeInformation().getName();
Map typeParameters = new HashMap();
if (typeName != null) {
// see if there is a corresponding type-def
final TypeDefinition typeDef = mappingDocument.getMetadataCollector().getTypeDefinition(typeName);
if (typeDef != null) {
typeName = typeDef.getTypeImplementorClass().getName();
if (typeDef.getParameters() != null) {
typeParameters.putAll(typeDef.getParameters());
}
} else {
// it could be a unqualified class name, in which case we should qualify
// it with the implicit package name for this context, if one.
typeName = mappingDocument.qualifyClassName(typeName);
}
}
if (source.getTypeInformation().getParameters() != null) {
typeParameters.putAll(source.getTypeInformation().getParameters());
}
binding.setTypeName(typeName);
binding.setTypeParameters(typeParameters);
if (source.getFetchCharacteristics().getFetchTiming() == FetchTiming.DELAYED) {
binding.setLazy(true);
binding.setExtraLazy(source.getFetchCharacteristics().isExtraLazy());
} else {
binding.setLazy(false);
}
switch(source.getFetchCharacteristics().getFetchStyle()) {
case SELECT:
{
binding.setFetchMode(FetchMode.SELECT);
break;
}
case JOIN:
{
binding.setFetchMode(FetchMode.JOIN);
break;
}
case BATCH:
{
binding.setFetchMode(FetchMode.SELECT);
binding.setBatchSize(source.getFetchCharacteristics().getBatchSize());
break;
}
case SUBSELECT:
{
binding.setFetchMode(FetchMode.SELECT);
binding.setSubselectLoadable(true);
// todo : this could totally be done using a "symbol map" approach
binding.getOwner().setSubselectLoadableCollections(true);
break;
}
default:
{
throw new AssertionFailure("Unexpected FetchStyle : " + source.getFetchCharacteristics().getFetchStyle().name());
}
}
for (String name : source.getSynchronizedTableNames()) {
binding.getSynchronizedTables().add(name);
}
binding.setWhere(source.getWhere());
binding.setLoaderName(source.getCustomLoaderName());
if (source.getCustomSqlInsert() != null) {
binding.setCustomSQLInsert(source.getCustomSqlInsert().getSql(), source.getCustomSqlInsert().isCallable(), source.getCustomSqlInsert().getCheckStyle());
}
if (source.getCustomSqlUpdate() != null) {
binding.setCustomSQLUpdate(source.getCustomSqlUpdate().getSql(), source.getCustomSqlUpdate().isCallable(), source.getCustomSqlUpdate().getCheckStyle());
}
if (source.getCustomSqlDelete() != null) {
binding.setCustomSQLDelete(source.getCustomSqlDelete().getSql(), source.getCustomSqlDelete().isCallable(), source.getCustomSqlDelete().getCheckStyle());
}
if (source.getCustomSqlDeleteAll() != null) {
binding.setCustomSQLDeleteAll(source.getCustomSqlDeleteAll().getSql(), source.getCustomSqlDeleteAll().isCallable(), source.getCustomSqlDeleteAll().getCheckStyle());
}
if (source instanceof Sortable) {
final Sortable sortable = (Sortable) source;
if (sortable.isSorted()) {
binding.setSorted(true);
if (!sortable.getComparatorName().equals("natural")) {
binding.setComparatorClassName(sortable.getComparatorName());
}
} else {
binding.setSorted(false);
}
}
if (source instanceof Orderable) {
if (((Orderable) source).isOrdered()) {
binding.setOrderBy(((Orderable) source).getOrder());
}
}
final String cascadeStyle = source.getCascadeStyleName();
if (cascadeStyle != null && cascadeStyle.contains("delete-orphan")) {
binding.setOrphanDelete(true);
}
for (FilterSource filterSource : source.getFilterSources()) {
String condition = filterSource.getCondition();
if (condition == null) {
final FilterDefinition filterDefinition = mappingDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
if (filterDefinition != null) {
condition = filterDefinition.getDefaultFilterCondition();
}
}
binding.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
}
}
use of org.hibernate.engine.spi.FilterDefinition in project hibernate-orm by hibernate.
the class AnnotationBinder method bindFilterDef.
private static void bindFilterDef(FilterDef defAnn, MetadataBuildingContext context) {
Map<String, org.hibernate.type.Type> params = new HashMap<>();
for (ParamDef param : defAnn.parameters()) {
params.put(param.name(), context.getMetadataCollector().getTypeResolver().heuristicType(param.type()));
}
FilterDefinition def = new FilterDefinition(defAnn.name(), defAnn.defaultCondition(), params);
LOG.debugf("Binding filter definition: %s", def.getFilterName());
context.getMetadataCollector().addFilterDefinition(def);
}
use of org.hibernate.engine.spi.FilterDefinition in project hibernate-orm by hibernate.
the class EntityBinder method bindEntity.
public void bindEntity() {
persistentClass.setAbstract(annotatedClass.isAbstract());
persistentClass.setClassName(annotatedClass.getName());
persistentClass.setJpaEntityName(name);
// persistentClass.setDynamic(false); //no longer needed with the Entity name refactoring?
persistentClass.setEntityName(annotatedClass.getName());
bindDiscriminatorValue();
persistentClass.setLazy(lazy);
if (proxyClass != null) {
persistentClass.setProxyInterfaceName(proxyClass.getName());
}
persistentClass.setDynamicInsert(dynamicInsert);
persistentClass.setDynamicUpdate(dynamicUpdate);
if (persistentClass instanceof RootClass) {
RootClass rootClass = (RootClass) persistentClass;
boolean mutable = true;
// priority on @Immutable, then @Entity.mutable()
if (annotatedClass.isAnnotationPresent(Immutable.class)) {
mutable = false;
} else {
org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
if (entityAnn != null) {
mutable = entityAnn.mutable();
}
}
rootClass.setMutable(mutable);
rootClass.setExplicitPolymorphism(isExplicitPolymorphism(polymorphismType));
if (StringHelper.isNotEmpty(where)) {
rootClass.setWhere(where);
}
if (cacheConcurrentStrategy != null) {
rootClass.setCacheConcurrencyStrategy(cacheConcurrentStrategy);
rootClass.setCacheRegionName(cacheRegion);
rootClass.setLazyPropertiesCacheable(cacheLazyProperty);
}
rootClass.setNaturalIdCacheRegionName(naturalIdCacheRegion);
boolean forceDiscriminatorInSelects = forceDiscriminator == null ? context.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect() : forceDiscriminator;
rootClass.setForceDiscriminator(forceDiscriminatorInSelects);
if (insertableDiscriminator != null) {
rootClass.setDiscriminatorInsertable(insertableDiscriminator);
}
} else {
if (explicitHibernateEntityAnnotation) {
LOG.entityAnnotationOnNonRoot(annotatedClass.getName());
}
if (annotatedClass.isAnnotationPresent(Immutable.class)) {
LOG.immutableAnnotationOnNonRoot(annotatedClass.getName());
}
}
persistentClass.setCached(isCached);
persistentClass.setOptimisticLockStyle(getVersioning(optimisticLockType));
persistentClass.setSelectBeforeUpdate(selectBeforeUpdate);
// set persister if needed
Persister persisterAnn = annotatedClass.getAnnotation(Persister.class);
Class persister = null;
if (persisterAnn != null) {
persister = persisterAnn.impl();
} else {
org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
if (entityAnn != null && !BinderHelper.isEmptyAnnotationValue(entityAnn.persister())) {
try {
persister = context.getBootstrapContext().getClassLoaderAccess().classForName(entityAnn.persister());
} catch (ClassLoadingException e) {
throw new AnnotationException("Could not find persister class: " + entityAnn.persister(), e);
}
}
}
if (persister != null) {
persistentClass.setEntityPersisterClass(persister);
}
persistentClass.setBatchSize(batchSize);
// SQL overriding
SQLInsert sqlInsert = annotatedClass.getAnnotation(SQLInsert.class);
SQLUpdate sqlUpdate = annotatedClass.getAnnotation(SQLUpdate.class);
SQLDelete sqlDelete = annotatedClass.getAnnotation(SQLDelete.class);
SQLDeleteAll sqlDeleteAll = annotatedClass.getAnnotation(SQLDeleteAll.class);
Loader loader = annotatedClass.getAnnotation(Loader.class);
if (sqlInsert != null) {
persistentClass.setCustomSQLInsert(sqlInsert.sql().trim(), sqlInsert.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlInsert.check().toString().toLowerCase(Locale.ROOT)));
}
if (sqlUpdate != null) {
persistentClass.setCustomSQLUpdate(sqlUpdate.sql(), sqlUpdate.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlUpdate.check().toString().toLowerCase(Locale.ROOT)));
}
if (sqlDelete != null) {
persistentClass.setCustomSQLDelete(sqlDelete.sql(), sqlDelete.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDelete.check().toString().toLowerCase(Locale.ROOT)));
}
if (sqlDeleteAll != null) {
persistentClass.setCustomSQLDelete(sqlDeleteAll.sql(), sqlDeleteAll.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDeleteAll.check().toString().toLowerCase(Locale.ROOT)));
}
if (loader != null) {
persistentClass.setLoaderName(loader.namedQuery());
}
final JdbcEnvironment jdbcEnvironment = context.getMetadataCollector().getDatabase().getJdbcEnvironment();
if (annotatedClass.isAnnotationPresent(Synchronize.class)) {
Synchronize synchronizedWith = annotatedClass.getAnnotation(Synchronize.class);
String[] tables = synchronizedWith.value();
for (String table : tables) {
persistentClass.addSynchronizedTable(context.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(table), jdbcEnvironment).render(jdbcEnvironment.getDialect()));
}
}
if (annotatedClass.isAnnotationPresent(Subselect.class)) {
Subselect subselect = annotatedClass.getAnnotation(Subselect.class);
this.subselect = subselect.value();
}
// tuplizers
if (annotatedClass.isAnnotationPresent(Tuplizers.class)) {
for (Tuplizer tuplizer : annotatedClass.getAnnotation(Tuplizers.class).value()) {
EntityMode mode = EntityMode.parse(tuplizer.entityMode());
// todo tuplizer.entityModeType
persistentClass.addTuplizer(mode, tuplizer.impl().getName());
}
}
if (annotatedClass.isAnnotationPresent(Tuplizer.class)) {
Tuplizer tuplizer = annotatedClass.getAnnotation(Tuplizer.class);
EntityMode mode = EntityMode.parse(tuplizer.entityMode());
// todo tuplizer.entityModeType
persistentClass.addTuplizer(mode, tuplizer.impl().getName());
}
for (Filter filter : filters) {
String filterName = filter.name();
String cond = filter.condition();
if (BinderHelper.isEmptyAnnotationValue(cond)) {
FilterDefinition definition = context.getMetadataCollector().getFilterDefinition(filterName);
cond = definition == null ? null : definition.getDefaultFilterCondition();
if (StringHelper.isEmpty(cond)) {
throw new AnnotationException("no filter condition found for filter " + filterName + " in " + this.name);
}
}
persistentClass.addFilter(filterName, cond, filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
LOG.debugf("Import with entity name %s", name);
try {
context.getMetadataCollector().addImport(name, persistentClass.getEntityName());
String entityName = persistentClass.getEntityName();
if (!entityName.equals(name)) {
context.getMetadataCollector().addImport(entityName, entityName);
}
} catch (MappingException me) {
throw new AnnotationException("Use of the same entity name twice: " + name, me);
}
processNamedEntityGraphs();
}
Aggregations