Search in sources :

Example 1 with AnnotationException

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

the class BinderHelper method makeIdGenerator.

/**
	 * apply an id generator to a SimpleValue
	 */
public static void makeIdGenerator(SimpleValue id, String generatorType, String generatorName, MetadataBuildingContext buildingContext, Map<String, IdentifierGeneratorDefinition> localGenerators) {
    Table table = id.getTable();
    table.setIdentifierValue(id);
    //generator settings
    id.setIdentifierGeneratorStrategy(generatorType);
    Properties params = new Properties();
    //always settable
    params.setProperty(PersistentIdentifierGenerator.TABLE, table.getName());
    final String implicitCatalogName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName();
    if (implicitCatalogName != null) {
        params.put(PersistentIdentifierGenerator.CATALOG, implicitCatalogName);
    }
    final String implicitSchemaName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName();
    if (implicitSchemaName != null) {
        params.put(PersistentIdentifierGenerator.SCHEMA, implicitSchemaName);
    }
    if (id.getColumnSpan() == 1) {
        params.setProperty(PersistentIdentifierGenerator.PK, ((org.hibernate.mapping.Column) id.getColumnIterator().next()).getName());
    }
    // YUCK!  but cannot think of a clean way to do this given the string-config based scheme
    params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, buildingContext.getObjectNameNormalizer());
    if (!isEmptyAnnotationValue(generatorName)) {
        //we have a named generator
        IdentifierGeneratorDefinition gen = getIdentifierGenerator(generatorName, localGenerators, buildingContext);
        if (gen == null) {
            throw new AnnotationException("Unknown Id.generator: " + generatorName);
        }
        //This is quite vague in the spec but a generator could override the generate choice
        String identifierGeneratorStrategy = gen.getStrategy();
        //yuk! this is a hack not to override 'AUTO' even if generator is set
        final boolean avoidOverriding = identifierGeneratorStrategy.equals("identity") || identifierGeneratorStrategy.equals("seqhilo") || identifierGeneratorStrategy.equals(MultipleHiLoPerTableGenerator.class.getName());
        if (generatorType == null || !avoidOverriding) {
            id.setIdentifierGeneratorStrategy(identifierGeneratorStrategy);
        }
        //checkIfMatchingGenerator(gen, generatorType, generatorName);
        for (Object o : gen.getParameters().entrySet()) {
            Map.Entry elt = (Map.Entry) o;
            params.setProperty((String) elt.getKey(), (String) elt.getValue());
        }
    }
    if ("assigned".equals(generatorType)) {
        id.setNullValue("undefined");
    }
    id.setIdentifierGeneratorProperties(params);
}
Also used : Table(org.hibernate.mapping.Table) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) AnnotationException(org.hibernate.AnnotationException) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AnnotationException

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

the class ListBinder method bindIndex.

private void bindIndex(final MetadataBuildingContext buildingContext) {
    if (!indexColumn.isImplicit()) {
        PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(this.collection, StringHelper.qualify(this.collection.getRole(), "key"), null, null, propertyHolder, getBuildingContext());
        List list = (List) this.collection;
        if (!list.isOneToMany())
            indexColumn.forceNotNull();
        indexColumn.setPropertyHolder(valueHolder);
        SimpleValueBinder value = new SimpleValueBinder();
        value.setColumns(new Ejb3Column[] { indexColumn });
        value.setExplicitType("integer");
        value.setBuildingContext(getBuildingContext());
        SimpleValue indexValue = value.make();
        indexColumn.linkWithValue(indexValue);
        list.setIndex(indexValue);
        list.setBaseIndex(indexColumn.getBase());
        if (list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse()) {
            String entityName = ((OneToMany) list.getElement()).getReferencedEntityName();
            PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
            IndexBackref ib = new IndexBackref();
            ib.setName('_' + propertyName + "IndexBackref");
            ib.setUpdateable(false);
            ib.setSelectable(false);
            ib.setCollectionRole(list.getRole());
            ib.setEntityName(list.getOwner().getEntityName());
            ib.setValue(list.getIndex());
            referenced.addProperty(ib);
        }
    } else {
        Collection coll = this.collection;
        throw new AnnotationException("List/array has to be annotated with an @OrderColumn (or @IndexColumn): " + coll.getRole());
    }
}
Also used : PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) AnnotationException(org.hibernate.AnnotationException) List(org.hibernate.mapping.List) IndexBackref(org.hibernate.mapping.IndexBackref) OneToMany(org.hibernate.mapping.OneToMany) SimpleValue(org.hibernate.mapping.SimpleValue) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with AnnotationException

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

the class QueryBinder method bindQuery.

public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, MetadataBuildingContext context) {
    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");
    }
    FlushMode flushMode;
    flushMode = getFlushMode(queryAnn.flushMode());
    NamedQueryDefinition query = new NamedQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).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(flushMode).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).createNamedQueryDefinition();
    context.getMetadataCollector().addNamedQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", query.getName(), query.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) FlushMode(org.hibernate.FlushMode)

Example 4 with AnnotationException

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

the class QueryHintDefinition method getLockMode.

public LockMode getLockMode(String query) {
    String hitName = QueryHints.NATIVE_LOCKMODE;
    String value = (String) hintsMap.get(hitName);
    if (value == null) {
        return null;
    }
    try {
        return LockMode.fromExternalForm(value);
    } catch (MappingException e) {
        throw new AnnotationException("Unknown LockMode in hint: " + query + ":" + hitName, e);
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) MappingException(org.hibernate.MappingException)

Example 5 with AnnotationException

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

the class EntityBinder method setCache.

public void setCache(Cache cacheAnn) {
    if (cacheAnn != null) {
        cacheRegion = BinderHelper.isEmptyAnnotationValue(cacheAnn.region()) ? null : cacheAnn.region();
        cacheConcurrentStrategy = getCacheConcurrencyStrategy(cacheAnn.usage());
        if ("all".equalsIgnoreCase(cacheAnn.include())) {
            cacheLazyProperty = true;
        } else if ("non-lazy".equalsIgnoreCase(cacheAnn.include())) {
            cacheLazyProperty = false;
        } else {
            throw new AnnotationException("Unknown lazy property annotations: " + cacheAnn.include());
        }
    } else {
        cacheConcurrentStrategy = null;
        cacheRegion = null;
        cacheLazyProperty = true;
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException)

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