use of org.hibernate.boot.model.TypeDefinition in project hibernate-orm by hibernate.
the class ModelBinder method resolveType.
private static TypeResolution resolveType(MappingDocument sourceDocument, HibernateTypeSource typeSource) {
if (StringHelper.isEmpty(typeSource.getName())) {
return null;
}
String typeName = typeSource.getName();
Properties typeParameters = new Properties();
;
final TypeDefinition typeDefinition = sourceDocument.getMetadataCollector().getTypeDefinition(typeName);
if (typeDefinition != null) {
// the explicit name referred to a type-def
typeName = typeDefinition.getTypeImplementorClass().getName();
if (typeDefinition.getParameters() != null) {
typeParameters.putAll(typeDefinition.getParameters());
}
}
// parameters on the property mapping should override parameters in the type-def
if (typeSource.getParameters() != null) {
typeParameters.putAll(typeSource.getParameters());
}
return new TypeResolution(typeName, typeParameters);
}
use of org.hibernate.boot.model.TypeDefinition in project hibernate-orm by hibernate.
the class TypeDefinitionBinder method processTypeDefinition.
/**
* Handling for a {@code <typedef/>} declaration
*
* @param context Access to information relative to the mapping document containing this binding
* @param typeDefinitionBinding The {@code <typedef/>} binding
*/
public static void processTypeDefinition(HbmLocalMetadataBuildingContext context, JaxbHbmTypeDefinitionType typeDefinitionBinding) {
final ClassLoaderService cls = context.getBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class);
final TypeDefinition definition = new TypeDefinition(typeDefinitionBinding.getName(), cls.classForName(typeDefinitionBinding.getClazz()), null, ConfigParameterHelper.extractConfigParameters(typeDefinitionBinding));
log.debugf("Processed type-definition : %s -> %s", definition.getName(), definition.getTypeImplementorClass().getName());
context.getMetadataCollector().addTypeDefinition(definition);
}
use of org.hibernate.boot.model.TypeDefinition in project hibernate-orm by hibernate.
the class CollectionBinder method getCollectionBinder.
/**
* collection binder factory
*/
public static CollectionBinder getCollectionBinder(String entityName, XProperty property, boolean isIndexed, boolean isHibernateExtensionMapping, MetadataBuildingContext buildingContext) {
final CollectionBinder result;
if (property.isArray()) {
if (property.getElementClass().isPrimitive()) {
result = new PrimitiveArrayBinder();
} else {
result = new ArrayBinder();
}
} else if (property.isCollection()) {
//TODO consider using an XClass
Class returnedClass = property.getCollectionClass();
if (java.util.Set.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new SetBinder(false);
} else if (java.util.SortedSet.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new SetBinder(true);
} else if (java.util.Map.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new MapBinder(false);
} else if (java.util.SortedMap.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new MapBinder(true);
} else if (java.util.Collection.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
result = new IdBagBinder();
} else {
result = new BagBinder();
}
} else if (java.util.List.class.equals(returnedClass)) {
if (isIndexed) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("List do not support @CollectionId and @OrderColumn (or @IndexColumn) at the same time: " + StringHelper.qualify(entityName, property.getName()));
}
result = new ListBinder();
} else if (property.isAnnotationPresent(CollectionId.class)) {
result = new IdBagBinder();
} else {
result = new BagBinder();
}
} else {
throw new AnnotationException(returnedClass.getName() + " collection not yet supported: " + StringHelper.qualify(entityName, property.getName()));
}
} else {
throw new AnnotationException("Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: " + StringHelper.qualify(entityName, property.getName()));
}
result.setIsHibernateExtensionMapping(isHibernateExtensionMapping);
final CollectionType typeAnnotation = property.getAnnotation(CollectionType.class);
if (typeAnnotation != null) {
final String typeName = typeAnnotation.type();
// see if it names a type-def
final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(typeName);
if (typeDef != null) {
result.explicitType = typeDef.getTypeImplementorClass().getName();
result.explicitTypeParameters.putAll(typeDef.getParameters());
} else {
result.explicitType = typeName;
for (Parameter param : typeAnnotation.parameters()) {
result.explicitTypeParameters.setProperty(param.name(), param.value());
}
}
}
return result;
}
use of org.hibernate.boot.model.TypeDefinition in project hibernate-orm by hibernate.
the class SimpleValueBinder method fillSimpleValue.
public void fillSimpleValue() {
LOG.debugf("Starting fillSimpleValue for %s", propertyName);
if (attributeConverterDescriptor != null) {
if (!BinderHelper.isEmptyAnnotationValue(explicitType)) {
throw new AnnotationException(String.format("AttributeConverter and explicit Type cannot be applied to same attribute [%s.%s];" + "remove @Type or specify @Convert(disableConversion = true)", persistentClassName, propertyName));
}
LOG.debugf("Applying JPA AttributeConverter [%s] to [%s:%s]", attributeConverterDescriptor, persistentClassName, propertyName);
simpleValue.setJpaAttributeConverterDescriptor(attributeConverterDescriptor);
} else {
String type;
TypeDefinition typeDef;
if (!BinderHelper.isEmptyAnnotationValue(explicitType)) {
type = explicitType;
typeDef = buildingContext.getMetadataCollector().getTypeDefinition(type);
} else {
// try implicit type
TypeDefinition implicitTypeDef = buildingContext.getMetadataCollector().getTypeDefinition(returnedClassName);
if (implicitTypeDef != null) {
typeDef = implicitTypeDef;
type = returnedClassName;
} else {
typeDef = buildingContext.getMetadataCollector().getTypeDefinition(defaultType);
type = defaultType;
}
}
if (typeDef != null) {
type = typeDef.getTypeImplementorClass().getName();
simpleValue.setTypeParameters(typeDef.getParametersAsProperties());
}
if (typeParameters != null && typeParameters.size() != 0) {
//explicit type params takes precedence over type def params
simpleValue.setTypeParameters(typeParameters);
}
simpleValue.setTypeName(type);
}
if (persistentClassName != null || attributeConverterDescriptor != null) {
try {
simpleValue.setTypeUsingReflection(persistentClassName, propertyName);
} catch (Exception e) {
throw new MappingException(String.format(Locale.ROOT, "Unable to determine basic type mapping via reflection [%s -> %s]", persistentClassName, propertyName), e);
}
}
if (!simpleValue.isTypeSpecified() && isVersion()) {
simpleValue.setTypeName("integer");
}
// HHH-5205
if (timeStampVersionType != null) {
simpleValue.setTypeName(timeStampVersionType);
}
if (simpleValue.getTypeName() != null && simpleValue.getTypeName().length() > 0 && simpleValue.getMetadata().getTypeResolver().basic(simpleValue.getTypeName()) == null) {
try {
Class typeClass = buildingContext.getClassLoaderAccess().classForName(simpleValue.getTypeName());
if (typeClass != null && DynamicParameterizedType.class.isAssignableFrom(typeClass)) {
Properties parameters = simpleValue.getTypeParameters();
if (parameters == null) {
parameters = new Properties();
}
parameters.put(DynamicParameterizedType.IS_DYNAMIC, Boolean.toString(true));
parameters.put(DynamicParameterizedType.RETURNED_CLASS, returnedClassName);
parameters.put(DynamicParameterizedType.IS_PRIMARY_KEY, Boolean.toString(key));
parameters.put(DynamicParameterizedType.ENTITY, persistentClassName);
parameters.put(DynamicParameterizedType.XPROPERTY, xproperty);
parameters.put(DynamicParameterizedType.PROPERTY, xproperty.getName());
parameters.put(DynamicParameterizedType.ACCESS_TYPE, accessType.getType());
simpleValue.setTypeParameters(parameters);
}
} catch (ClassLoadingException e) {
throw new MappingException("Could not determine type for: " + simpleValue.getTypeName(), e);
}
}
}
use of org.hibernate.boot.model.TypeDefinition 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.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());
}
}
Aggregations