Search in sources :

Example 1 with SQLUpdate

use of org.hibernate.annotations.SQLUpdate 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());
        }
        if (annotatedClass.isAnnotationPresent(Cacheable.class) || annotatedClass.isAnnotationPresent(Cache.class)) {
            LOG.cacheOrCacheableAnnotationOnNonRoot(annotatedClass.getName());
        }
    }
    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.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();
}
Also used : Cacheable(javax.persistence.Cacheable) Synchronize(org.hibernate.annotations.Synchronize) Loader(org.hibernate.annotations.Loader) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) MappingException(org.hibernate.MappingException) FilterDefinition(org.hibernate.engine.spi.FilterDefinition) AnnotationException(org.hibernate.AnnotationException) SQLInsert(org.hibernate.annotations.SQLInsert) Persister(org.hibernate.annotations.Persister) RootClass(org.hibernate.mapping.RootClass) Tuplizer(org.hibernate.annotations.Tuplizer) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Subselect(org.hibernate.annotations.Subselect) SQLDelete(org.hibernate.annotations.SQLDelete) EntityMode(org.hibernate.EntityMode) Filter(org.hibernate.annotations.Filter) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) XClass(org.hibernate.annotations.common.reflection.XClass) SQLUpdate(org.hibernate.annotations.SQLUpdate) SQLDeleteAll(org.hibernate.annotations.SQLDeleteAll) Tuplizers(org.hibernate.annotations.Tuplizers) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) Cache(org.hibernate.annotations.Cache)

Example 2 with SQLUpdate

use of org.hibernate.annotations.SQLUpdate in project hibernate-orm by hibernate.

the class CollectionBinder method bind.

public void bind() {
    this.collection = createCollection(propertyHolder.getPersistentClass());
    String role = StringHelper.qualify(propertyHolder.getPath(), propertyName);
    LOG.debugf("Collection role: %s", role);
    collection.setRole(role);
    collection.setMappedByProperty(mappedBy);
    if (property.isAnnotationPresent(MapKeyColumn.class) && mapKeyPropertyName != null) {
        throw new AnnotationException("Cannot mix @javax.persistence.MapKey and @MapKeyColumn or @org.hibernate.annotations.MapKey " + "on the same collection: " + StringHelper.qualify(propertyHolder.getPath(), propertyName));
    }
    // set explicit type information
    if (explicitType != null) {
        final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(explicitType);
        if (typeDef == null) {
            collection.setTypeName(explicitType);
            collection.setTypeParameters(explicitTypeParameters);
        } else {
            collection.setTypeName(typeDef.getTypeImplementorClass().getName());
            collection.setTypeParameters(typeDef.getParameters());
        }
    }
    //set laziness
    defineFetchingStrategy();
    collection.setBatchSize(batchSize);
    collection.setMutable(!property.isAnnotationPresent(Immutable.class));
    //work on association
    boolean isMappedBy = !BinderHelper.isEmptyAnnotationValue(mappedBy);
    final OptimisticLock lockAnn = property.getAnnotation(OptimisticLock.class);
    final boolean includeInOptimisticLockChecks = (lockAnn != null) ? !lockAnn.excluded() : !isMappedBy;
    collection.setOptimisticLocked(includeInOptimisticLockChecks);
    Persister persisterAnn = property.getAnnotation(Persister.class);
    if (persisterAnn != null) {
        collection.setCollectionPersisterClass(persisterAnn.impl());
    }
    applySortingAndOrdering(collection);
    //set cache
    if (StringHelper.isNotEmpty(cacheConcurrencyStrategy)) {
        collection.setCacheConcurrencyStrategy(cacheConcurrencyStrategy);
        collection.setCacheRegionName(cacheRegionName);
    }
    //SQL overriding
    SQLInsert sqlInsert = property.getAnnotation(SQLInsert.class);
    SQLUpdate sqlUpdate = property.getAnnotation(SQLUpdate.class);
    SQLDelete sqlDelete = property.getAnnotation(SQLDelete.class);
    SQLDeleteAll sqlDeleteAll = property.getAnnotation(SQLDeleteAll.class);
    Loader loader = property.getAnnotation(Loader.class);
    if (sqlInsert != null) {
        collection.setCustomSQLInsert(sqlInsert.sql().trim(), sqlInsert.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlInsert.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlUpdate != null) {
        collection.setCustomSQLUpdate(sqlUpdate.sql(), sqlUpdate.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlUpdate.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDelete != null) {
        collection.setCustomSQLDelete(sqlDelete.sql(), sqlDelete.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDelete.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDeleteAll != null) {
        collection.setCustomSQLDeleteAll(sqlDeleteAll.sql(), sqlDeleteAll.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDeleteAll.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (loader != null) {
        collection.setLoaderName(loader.namedQuery());
    }
    if (isMappedBy && (property.isAnnotationPresent(JoinColumn.class) || property.isAnnotationPresent(JoinColumns.class) || propertyHolder.getJoinTable(property) != null)) {
        String message = "Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: ";
        message += StringHelper.qualify(propertyHolder.getPath(), propertyName);
        throw new AnnotationException(message);
    }
    collection.setInverse(isMappedBy);
    //many to many may need some second pass informations
    if (!oneToMany && isMappedBy) {
        buildingContext.getMetadataCollector().addMappedBy(getCollectionType().getName(), mappedBy, propertyName);
    }
    //TODO reducce tableBinder != null and oneToMany
    XClass collectionType = getCollectionType();
    if (inheritanceStatePerClass == null)
        throw new AssertionFailure("inheritanceStatePerClass not set");
    SecondPass sp = getSecondPass(fkJoinColumns, joinColumns, inverseJoinColumns, elementColumns, mapKeyColumns, mapKeyManyToManyColumns, isEmbedded, property, collectionType, ignoreNotFound, oneToMany, tableBinder, buildingContext);
    if (collectionType.isAnnotationPresent(Embeddable.class) || //JPA 2
    property.isAnnotationPresent(ElementCollection.class)) {
        // do it right away, otherwise @ManyToOne on composite element call addSecondPass
        // and raise a ConcurrentModificationException
        //sp.doSecondPass( CollectionHelper.EMPTY_MAP );
        buildingContext.getMetadataCollector().addSecondPass(sp, !isMappedBy);
    } else {
        buildingContext.getMetadataCollector().addSecondPass(sp, !isMappedBy);
    }
    buildingContext.getMetadataCollector().addCollectionBinding(collection);
    //property building
    PropertyBinder binder = new PropertyBinder();
    binder.setName(propertyName);
    binder.setValue(collection);
    binder.setCascade(cascadeStrategy);
    if (cascadeStrategy != null && cascadeStrategy.indexOf("delete-orphan") >= 0) {
        collection.setOrphanDelete(true);
    }
    binder.setLazy(collection.isLazy());
    final LazyGroup lazyGroupAnnotation = property.getAnnotation(LazyGroup.class);
    if (lazyGroupAnnotation != null) {
        binder.setLazyGroup(lazyGroupAnnotation.value());
    }
    binder.setAccessType(accessType);
    binder.setProperty(property);
    binder.setInsertable(insertable);
    binder.setUpdatable(updatable);
    Property prop = binder.makeProperty();
    //we don't care about the join stuffs because the column is on the association table.
    if (!declaringClassSet)
        throw new AssertionFailure("DeclaringClass is not set in CollectionBinder while binding");
    propertyHolder.addProperty(prop, declaringClass);
}
Also used : AssertionFailure(org.hibernate.annotations.common.AssertionFailure) MapKeyColumn(javax.persistence.MapKeyColumn) Loader(org.hibernate.annotations.Loader) SQLDelete(org.hibernate.annotations.SQLDelete) XClass(org.hibernate.annotations.common.reflection.XClass) TypeDefinition(org.hibernate.boot.model.TypeDefinition) Embeddable(javax.persistence.Embeddable) SecondPass(org.hibernate.cfg.SecondPass) CollectionSecondPass(org.hibernate.cfg.CollectionSecondPass) LazyGroup(org.hibernate.annotations.LazyGroup) AnnotationException(org.hibernate.AnnotationException) SQLInsert(org.hibernate.annotations.SQLInsert) SQLUpdate(org.hibernate.annotations.SQLUpdate) SQLDeleteAll(org.hibernate.annotations.SQLDeleteAll) Persister(org.hibernate.annotations.Persister) ElementCollection(javax.persistence.ElementCollection) OptimisticLock(org.hibernate.annotations.OptimisticLock) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Aggregations

AnnotationException (org.hibernate.AnnotationException)2 Loader (org.hibernate.annotations.Loader)2 Persister (org.hibernate.annotations.Persister)2 SQLDelete (org.hibernate.annotations.SQLDelete)2 SQLDeleteAll (org.hibernate.annotations.SQLDeleteAll)2 SQLInsert (org.hibernate.annotations.SQLInsert)2 SQLUpdate (org.hibernate.annotations.SQLUpdate)2 XClass (org.hibernate.annotations.common.reflection.XClass)2 Cacheable (javax.persistence.Cacheable)1 ElementCollection (javax.persistence.ElementCollection)1 Embeddable (javax.persistence.Embeddable)1 MapKeyColumn (javax.persistence.MapKeyColumn)1 EntityMode (org.hibernate.EntityMode)1 MappingException (org.hibernate.MappingException)1 Cache (org.hibernate.annotations.Cache)1 Filter (org.hibernate.annotations.Filter)1 LazyGroup (org.hibernate.annotations.LazyGroup)1 NaturalIdCache (org.hibernate.annotations.NaturalIdCache)1 OptimisticLock (org.hibernate.annotations.OptimisticLock)1 Subselect (org.hibernate.annotations.Subselect)1