Search in sources :

Example 71 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class AnnotationBinder method bindComponent.

private static PropertyBinder bindComponent(PropertyData inferredData, PropertyHolder propertyHolder, AccessType propertyAccessor, EntityBinder entityBinder, boolean isIdentifierMapper, MetadataBuildingContext buildingContext, boolean isComponentEmbedded, // is a identifier
boolean isId, Map<XClass, InheritanceState> inheritanceStatePerClass, // is a component who is overridden by a @MapsId
String referencedEntityName, Ejb3JoinColumn[] columns) {
    Component comp;
    if (referencedEntityName != null) {
        comp = createComponent(propertyHolder, inferredData, isComponentEmbedded, isIdentifierMapper, buildingContext);
        SecondPass sp = new CopyIdentifierComponentSecondPass(comp, referencedEntityName, columns, buildingContext);
        buildingContext.getMetadataCollector().addSecondPass(sp);
    } else {
        comp = fillComponent(propertyHolder, inferredData, propertyAccessor, !isId, entityBinder, isComponentEmbedded, isIdentifierMapper, false, buildingContext, inheritanceStatePerClass);
    }
    if (isId) {
        comp.setKey(true);
        if (propertyHolder.getPersistentClass().getIdentifier() != null) {
            throw new AnnotationException(comp.getComponentClassName() + " must not have @Id properties when used as an @EmbeddedId: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
        if (referencedEntityName == null && comp.getPropertySpan() == 0) {
            throw new AnnotationException(comp.getComponentClassName() + " has no persistent id property: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
    }
    XProperty property = inferredData.getProperty();
    setupComponentTuplizer(property, comp);
    PropertyBinder binder = new PropertyBinder();
    binder.setDeclaringClass(inferredData.getDeclaringClass());
    binder.setName(inferredData.getPropertyName());
    binder.setValue(comp);
    binder.setProperty(inferredData.getProperty());
    binder.setAccessType(inferredData.getDefaultAccess());
    binder.setEmbedded(isComponentEmbedded);
    binder.setHolder(propertyHolder);
    binder.setId(isId);
    binder.setEntityBinder(entityBinder);
    binder.setInheritanceStatePerClass(inheritanceStatePerClass);
    binder.setBuildingContext(buildingContext);
    binder.makePropertyAndBind();
    return binder;
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) AnnotationException(org.hibernate.AnnotationException) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Component(org.hibernate.mapping.Component)

Example 72 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(NamedNativeQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
    if (queryAnn == null)
        return;
    // ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    String resultSetMapping = queryAnn.resultSetMapping();
    QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
    String queryName = queryAnn.query();
    NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(queryAnn.name()).setQuery(queryName).setQuerySpaces(null).setCacheable(hints.getBoolean(queryName, QueryHints.CACHEABLE)).setCacheRegion(hints.getString(queryName, QueryHints.CACHE_REGION)).setTimeout(hints.getTimeout(queryName)).setFetchSize(hints.getInteger(queryName, QueryHints.FETCH_SIZE)).setFlushMode(hints.getFlushMode(queryName)).setCacheMode(hints.getCacheMode(queryName)).setReadOnly(hints.getBoolean(queryName, QueryHints.READ_ONLY)).setComment(hints.getString(queryName, QueryHints.COMMENT)).setParameterTypes(null).setCallable(hints.getBoolean(queryName, QueryHints.CALLABLE));
    if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
        // sql result set usage
        builder.setResultSetRef(resultSetMapping).createNamedQueryDefinition();
    } else if (!void.class.equals(queryAnn.resultClass())) {
        // class mapping usage
        // FIXME should be done in a second pass due to entity name?
        final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
        builder.setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn });
    } else {
        builder.setQueryReturns(new NativeSQLQueryReturn[0]);
    }
    NamedSQLQueryDefinition query = builder.createNamedQueryDefinition();
    if (isDefault) {
        context.getMetadataCollector().addDefaultNamedNativeQuery(query);
    } else {
        context.getMetadataCollector().addNamedNativeQuery(query);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", queryAnn.name(), queryAnn.query());
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) AnnotationException(org.hibernate.AnnotationException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder)

Example 73 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    // ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    NamedSQLQueryDefinition query;
    String resultSetMapping = queryAnn.resultSetMapping();
    if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
        // sql result set usage
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setResultSetRef(resultSetMapping).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else if (!void.class.equals(queryAnn.resultClass())) {
        // class mapping usage
        // FIXME should be done in a second pass due to entity name?
        final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn }).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else {
        throw new NotYetImplementedException("Pure native scalar queries are not yet supported");
    }
    context.getMetadataCollector().addNamedNativeQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", query.getName(), queryAnn.query());
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) AnnotationException(org.hibernate.AnnotationException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) NotYetImplementedException(org.hibernate.cfg.NotYetImplementedException)

Example 74 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class QueryBinder method bindQuery.

public static void bindQuery(NamedQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
    if (queryAnn == null)
        return;
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    // EJBQL Query
    QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
    String queryName = queryAnn.query();
    NamedQueryDefinition queryDefinition = new NamedQueryDefinitionBuilder(queryAnn.name()).setLockOptions(hints.determineLockOptions(queryAnn)).setQuery(queryName).setCacheable(hints.getBoolean(queryName, QueryHints.CACHEABLE)).setCacheRegion(hints.getString(queryName, QueryHints.CACHE_REGION)).setTimeout(hints.getTimeout(queryName)).setFetchSize(hints.getInteger(queryName, QueryHints.FETCH_SIZE)).setFlushMode(hints.getFlushMode(queryName)).setCacheMode(hints.getCacheMode(queryName)).setReadOnly(hints.getBoolean(queryName, QueryHints.READ_ONLY)).setComment(hints.getString(queryName, QueryHints.COMMENT)).setParameterTypes(null).createNamedQueryDefinition();
    if (isDefault) {
        context.getMetadataCollector().addDefaultQuery(queryDefinition);
    } else {
        context.getMetadataCollector().addNamedQuery(queryDefinition);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", queryDefinition.getName(), queryDefinition.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder)

Example 75 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class CollectionBinder method applySortingAndOrdering.

private void applySortingAndOrdering(Collection collection) {
    boolean hadOrderBy = false;
    boolean hadExplicitSort = false;
    Class<? extends Comparator> comparatorClass = null;
    if (jpaOrderBy == null && sqlOrderBy == null) {
        if (deprecatedSort != null) {
            LOG.debug("Encountered deprecated @Sort annotation; use @SortNatural or @SortComparator instead.");
            if (naturalSort != null || comparatorSort != null) {
                throw buildIllegalSortCombination();
            }
            hadExplicitSort = deprecatedSort.type() != SortType.UNSORTED;
            if (deprecatedSort.type() == SortType.NATURAL) {
                isSortedCollection = true;
            } else if (deprecatedSort.type() == SortType.COMPARATOR) {
                isSortedCollection = true;
                comparatorClass = deprecatedSort.comparator();
            }
        } else if (naturalSort != null) {
            if (comparatorSort != null) {
                throw buildIllegalSortCombination();
            }
            hadExplicitSort = true;
        } else if (comparatorSort != null) {
            hadExplicitSort = true;
            comparatorClass = comparatorSort.value();
        }
    } else {
        if (jpaOrderBy != null && sqlOrderBy != null) {
            throw new AnnotationException(String.format("Illegal combination of @%s and @%s on %s", javax.persistence.OrderBy.class.getName(), OrderBy.class.getName(), safeCollectionRole()));
        }
        hadOrderBy = true;
        hadExplicitSort = false;
        // we can only apply the sql-based order by up front.  The jpa order by has to wait for second pass
        if (sqlOrderBy != null) {
            collection.setOrderBy(sqlOrderBy.clause());
        }
    }
    if (isSortedCollection) {
        if (!hadExplicitSort && !hadOrderBy) {
            throw new AnnotationException("A sorted collection must define and ordering or sorting : " + safeCollectionRole());
        }
    }
    collection.setSorted(isSortedCollection || hadExplicitSort);
    if (comparatorClass != null) {
        try {
            collection.setComparator(comparatorClass.newInstance());
        } catch (Exception e) {
            throw new AnnotationException(String.format("Could not instantiate comparator class [%s] for %s", comparatorClass.getName(), safeCollectionRole()));
        }
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) AnnotationException(org.hibernate.AnnotationException) MappingException(org.hibernate.MappingException)

Aggregations

AnnotationException (org.hibernate.AnnotationException)85 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)15 PersistentClass (org.hibernate.mapping.PersistentClass)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)12 Element (org.dom4j.Element)12 XClass (org.hibernate.annotations.common.reflection.XClass)12 HashMap (java.util.HashMap)11 MappingException (org.hibernate.MappingException)11 XProperty (org.hibernate.annotations.common.reflection.XProperty)11 Property (org.hibernate.mapping.Property)11 SimpleValue (org.hibernate.mapping.SimpleValue)11 Test (org.junit.Test)10 AssertionFailure (org.hibernate.AssertionFailure)9 ArrayList (java.util.ArrayList)8 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)8 Column (org.hibernate.mapping.Column)8 IdClass (javax.persistence.IdClass)7 MapKeyClass (javax.persistence.MapKeyClass)7 Component (org.hibernate.mapping.Component)7 Properties (java.util.Properties)6