Search in sources :

Example 1 with SqmMultiTableInsertStrategy

use of org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy in project hibernate-orm by hibernate.

the class SqmMutationStrategyHelper method resolveInsertStrategy.

/**
 * Standard resolution of SqmInsertStrategy to use for a given
 * entity hierarchy.
 */
public static SqmMultiTableInsertStrategy resolveInsertStrategy(RootClass entityBootDescriptor, EntityMappingType rootEntityDescriptor, MappingModelCreationProcess creationProcess) {
    final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
    final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
    final SessionFactoryOptions options = sessionFactory.getSessionFactoryOptions();
    final SqmMultiTableInsertStrategy specifiedStrategy = options.getCustomSqmMultiTableInsertStrategy();
    if (specifiedStrategy != null) {
        return specifiedStrategy;
    }
    return sessionFactory.getServiceRegistry().getService(JdbcServices.class).getJdbcEnvironment().getDialect().getFallbackSqmInsertStrategy(rootEntityDescriptor, creationContext);
}
Also used : RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqmMultiTableInsertStrategy(org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 2 with SqmMultiTableInsertStrategy

use of org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy in project hibernate-orm by hibernate.

the class SessionFactoryOptionsBuilder method resolveSqmInsertStrategy.

@SuppressWarnings("unchecked")
private SqmMultiTableInsertStrategy resolveSqmInsertStrategy(String strategyName, StandardServiceRegistry serviceRegistry, StrategySelector strategySelector) {
    if (strategyName == null) {
        return null;
    }
    return strategySelector.resolveStrategy(SqmMultiTableInsertStrategy.class, strategyName, (SqmMultiTableInsertStrategy) null, strategyClass -> {
        Constructor<SqmMultiTableInsertStrategy> dialectConstructor = null;
        Constructor<SqmMultiTableInsertStrategy> emptyConstructor = null;
        // todo (6.0) : formalize the allowed constructor parameterizations
        for (Constructor<?> declaredConstructor : strategyClass.getDeclaredConstructors()) {
            final Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            if (parameterTypes.length == 1 && parameterTypes[0] == Dialect.class) {
                dialectConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
                break;
            } else if (parameterTypes.length == 0) {
                emptyConstructor = (Constructor<SqmMultiTableInsertStrategy>) declaredConstructor;
            }
        }
        try {
            if (dialectConstructor != null) {
                return dialectConstructor.newInstance(serviceRegistry.getService(JdbcServices.class).getDialect());
            } else if (emptyConstructor != null) {
                return emptyConstructor.newInstance();
            }
        } catch (Exception e) {
            throw new StrategySelectionException(String.format("Could not instantiate named strategy class [%s]", strategyClass.getName()), e);
        }
        throw new IllegalArgumentException("Cannot instantiate the class [" + strategyClass.getName() + "] because it does not have a constructor that accepts a dialect or an empty constructor!");
    });
}
Also used : StrategySelectionException(org.hibernate.boot.registry.selector.spi.StrategySelectionException) Constructor(java.lang.reflect.Constructor) SqmMultiTableInsertStrategy(org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy) Dialect(org.hibernate.dialect.Dialect) HibernateException(org.hibernate.HibernateException) StrategySelectionException(org.hibernate.boot.registry.selector.spi.StrategySelectionException)

Example 3 with SqmMultiTableInsertStrategy

use of org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy in project hibernate-orm by hibernate.

the class QuerySqmImpl method buildInsertQueryPlan.

private NonSelectQueryPlan buildInsertQueryPlan() {
    // noinspection rawtypes
    final SqmInsertStatement sqmInsert = (SqmInsertStatement) getSqmStatement();
    final String entityNameToInsert = sqmInsert.getTarget().getReferencedPathSource().getHibernateEntityName();
    final EntityPersister entityDescriptor = getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityNameToInsert);
    final SqmMultiTableInsertStrategy multiTableStrategy = entityDescriptor.getSqmMultiTableInsertStrategy();
    if (multiTableStrategy == null || isSimpleValuesInsert(sqmInsert, entityDescriptor)) {
        return new SimpleInsertQueryPlan(sqmInsert, domainParameterXref);
    } else {
        return new MultiTableInsertQueryPlan(sqmInsert, domainParameterXref, multiTableStrategy);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SqmMultiTableInsertStrategy(org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy) SqmInsertStatement(org.hibernate.query.sqm.tree.insert.SqmInsertStatement)

Aggregations

SqmMultiTableInsertStrategy (org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy)3 Constructor (java.lang.reflect.Constructor)1 HibernateException (org.hibernate.HibernateException)1 StrategySelectionException (org.hibernate.boot.registry.selector.spi.StrategySelectionException)1 SessionFactoryOptions (org.hibernate.boot.spi.SessionFactoryOptions)1 Dialect (org.hibernate.dialect.Dialect)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 RuntimeModelCreationContext (org.hibernate.metamodel.spi.RuntimeModelCreationContext)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 SqmInsertStatement (org.hibernate.query.sqm.tree.insert.SqmInsertStatement)1