use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class XMLContext method setLocalAttributeConverterDefinitions.
@SuppressWarnings("unchecked")
private void setLocalAttributeConverterDefinitions(List<Element> converterElements) {
for (Element converterElement : converterElements) {
final String className = converterElement.attributeValue("class");
final String autoApplyAttribute = converterElement.attributeValue("auto-apply");
final boolean autoApply = autoApplyAttribute != null && Boolean.parseBoolean(autoApplyAttribute);
try {
final Class<? extends AttributeConverter> attributeConverterClass = classLoaderAccess.classForName(className);
attributeConverterInfoList.add(new AttributeConverterDefinition(attributeConverterClass.newInstance(), autoApply));
} catch (ClassLoadingException e) {
throw new AnnotationException("Unable to locate specified AttributeConverter implementation class : " + className, e);
} catch (Exception e) {
throw new AnnotationException("Unable to instantiate specified AttributeConverter implementation class : " + className, e);
}
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class ToOneFkSecondPass method doSecondPass.
public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
if (value instanceof ManyToOne) {
ManyToOne manyToOne = (ManyToOne) value;
PersistentClass ref = (PersistentClass) persistentClasses.get(manyToOne.getReferencedEntityName());
if (ref == null) {
throw new AnnotationException("@OneToOne or @ManyToOne on " + StringHelper.qualify(entityClassName, path) + " references an unknown entity: " + manyToOne.getReferencedEntityName());
}
manyToOne.setPropertyName(path);
BinderHelper.createSyntheticPropertyReference(columns, ref, null, manyToOne, false, buildingContext);
TableBinder.bindFk(ref, null, columns, manyToOne, unique, buildingContext);
/*
* HbmMetadataSourceProcessorImpl does this only when property-ref != null, but IMO, it makes sense event if it is null
*/
if (!manyToOne.isIgnoreNotFound())
manyToOne.createPropertyRefConstraints(persistentClasses);
} else if (value instanceof OneToOne) {
value.createForeignKey();
} else {
throw new AssertionFailure("FkSecondPass for a wrong value type: " + value.getClass().getName());
}
}
use of org.hibernate.AnnotationException 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.AnnotationException in project hibernate-orm by hibernate.
the class CollectionBinder method bindFilters.
private void bindFilters(boolean hasAssociationTable) {
Filter simpleFilter = property.getAnnotation(Filter.class);
// if ( StringHelper.isNotEmpty( where ) ) collection.setWhere( where );
if (simpleFilter != null) {
if (hasAssociationTable) {
collection.addManyToManyFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
} else {
collection.addFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
}
}
Filters filters = property.getAnnotation(Filters.class);
if (filters != null) {
for (Filter filter : filters.value()) {
if (hasAssociationTable) {
collection.addManyToManyFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
} else {
collection.addFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
}
}
FilterJoinTable simpleFilterJoinTable = property.getAnnotation(FilterJoinTable.class);
if (simpleFilterJoinTable != null) {
if (hasAssociationTable) {
collection.addFilter(simpleFilterJoinTable.name(), simpleFilterJoinTable.condition(), simpleFilterJoinTable.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilterJoinTable.aliases()), toAliasEntityMap(simpleFilterJoinTable.aliases()));
} else {
throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
}
}
FilterJoinTables filterJoinTables = property.getAnnotation(FilterJoinTables.class);
if (filterJoinTables != null) {
for (FilterJoinTable filter : filterJoinTables.value()) {
if (hasAssociationTable) {
collection.addFilter(filter.name(), filter.condition(), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
} else {
throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
}
}
}
StringBuilder whereBuffer = new StringBuilder();
if (property.getElementClass() != null) {
Where whereOnClass = property.getElementClass().getAnnotation(Where.class);
if (whereOnClass != null) {
String clause = whereOnClass.clause();
if (StringHelper.isNotEmpty(clause)) {
whereBuffer.append(clause);
}
}
}
Where whereOnCollection = property.getAnnotation(Where.class);
if (whereOnCollection != null) {
String clause = whereOnCollection.clause();
if (StringHelper.isNotEmpty(clause)) {
if (whereBuffer.length() > 0) {
whereBuffer.append(' ');
whereBuffer.append(Junction.Nature.AND.getOperator());
whereBuffer.append(' ');
}
whereBuffer.append(clause);
}
}
if (whereBuffer.length() > 0) {
String whereClause = whereBuffer.toString();
if (hasAssociationTable) {
collection.setManyToManyWhere(whereClause);
} else {
collection.setWhere(whereClause);
}
}
WhereJoinTable whereJoinTable = property.getAnnotation(WhereJoinTable.class);
String whereJoinTableClause = whereJoinTable == null ? null : whereJoinTable.clause();
if (StringHelper.isNotEmpty(whereJoinTableClause)) {
if (hasAssociationTable) {
collection.setWhere(whereJoinTableClause);
} else {
throw new AnnotationException("Illegal use of @WhereJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
}
}
// This cannot happen in annotations since the second fetch is hardcoded to join
// if ( ( ! collection.getManyToManyFilterMap().isEmpty() || collection.getManyToManyWhere() != null ) &&
// collection.getFetchMode() == FetchMode.JOIN &&
// collection.getElement().getFetchMode() != FetchMode.JOIN ) {
// throw new MappingException(
// "association with join table defining filter or where without join fetching " +
// "not valid within collection using join fetching [" + collection.getRole() + "]"
// );
// }
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class CollectionBinder method bindStarToManySecondPass.
/**
* return true if it's a Fk, false if it's an association table
*/
protected boolean bindStarToManySecondPass(Map persistentClasses, XClass collType, Ejb3JoinColumn[] fkJoinColumns, Ejb3JoinColumn[] keyColumns, Ejb3JoinColumn[] inverseColumns, Ejb3Column[] elementColumns, boolean isEmbedded, XProperty property, boolean unique, TableBinder associationTableBinder, boolean ignoreNotFound, MetadataBuildingContext buildingContext) {
PersistentClass persistentClass = (PersistentClass) persistentClasses.get(collType.getName());
boolean reversePropertyInJoin = false;
if (persistentClass != null && StringHelper.isNotEmpty(this.mappedBy)) {
try {
reversePropertyInJoin = 0 != persistentClass.getJoinNumber(persistentClass.getRecursiveProperty(this.mappedBy));
} catch (MappingException e) {
throw new AnnotationException("mappedBy reference an unknown target entity property: " + collType + "." + this.mappedBy + " in " + collection.getOwnerEntityName() + "." + property.getName());
}
}
if (persistentClass != null && !reversePropertyInJoin && oneToMany && !this.isExplicitAssociationTable && (// implicit @JoinColumn
joinColumns[0].isImplicit() && !BinderHelper.isEmptyAnnotationValue(this.mappedBy) || // this is an explicit @JoinColumn
!fkJoinColumns[0].isImplicit())) {
// this is a Foreign key
bindOneToManySecondPass(getCollection(), persistentClasses, fkJoinColumns, collType, cascadeDeleteEnabled, ignoreNotFound, buildingContext, inheritanceStatePerClass);
return true;
} else {
// this is an association table
bindManyToManySecondPass(this.collection, persistentClasses, keyColumns, inverseColumns, elementColumns, isEmbedded, collType, ignoreNotFound, unique, cascadeDeleteEnabled, associationTableBinder, property, propertyHolder, buildingContext);
return false;
}
}
Aggregations