Example usage for org.hibernate.engine.spi SessionFactoryImplementor getServiceRegistry

List of usage examples for org.hibernate.engine.spi SessionFactoryImplementor getServiceRegistry

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SessionFactoryImplementor getServiceRegistry.

Prototype

ServiceRegistryImplementor getServiceRegistry();

Source Link

Document

Access to the ServiceRegistry for this SessionFactory.

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public String[] getColumnTypes(EntityType<?> entityType, String attributeName) {
    QueryableCollection collectionPersister = getCollectionPersister(entityType, attributeName);
    if (collectionPersister == null) {
        AbstractEntityPersister entityPersister = getEntityPersister(entityType);
        SessionFactoryImplementor sfi = entityPersister.getFactory();
        String[] columnNames = entityPersister.getPropertyColumnNames(attributeName);
        Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService();
        Table[] tables;//from   ww w  .j  a v a2  s .  co m

        if (entityPersister instanceof JoinedSubclassEntityPersister) {
            tables = new Table[((JoinedSubclassEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(entityPersister.getSubclassTableName(i));
            }
        } else if (entityPersister instanceof UnionSubclassEntityPersister) {
            tables = new Table[((UnionSubclassEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i)));
            }
        } else if (entityPersister instanceof SingleTableEntityPersister) {
            tables = new Table[((SingleTableEntityPersister) entityPersister).getSubclassTableSpan()];
            for (int i = 0; i < tables.length; i++) {
                tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i)));
            }
        } else {
            tables = new Table[] { database.getTable(unquote(entityPersister.getTableName())) };
        }

        // In this case, the property might represent a formula
        boolean isFormula = columnNames.length == 1 && columnNames[0] == null;
        boolean isSubselect = tables.length == 1 && tables[0] == null;

        if (isFormula || isSubselect) {
            Type propertyType = entityPersister.getPropertyType(attributeName);
            return getColumnTypeForPropertyType(entityType, attributeName, sfi, propertyType);
        }

        return getColumnTypesForColumnNames(entityType, columnNames, tables);
    } else {
        SessionFactoryImplementor sfi = collectionPersister.getFactory();
        return getColumnTypeForPropertyType(entityType, attributeName, sfi,
                collectionPersister.getElementType());
    }
}

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java

License:Apache License

@Override
public String[] getColumnTypes(EntityType<?> ownerType, String elementCollectionPath, String attributeName) {
    QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath);
    SessionFactoryImplementor sfi = persister.getFactory();
    String[] columnNames = null;//w  ww .j  a  v  a  2 s.c  om
    Type propertyType = null;
    String subAttributeName = attributeName.substring(elementCollectionPath.length() + 1);
    if (persister.getElementType() instanceof ComponentType) {
        ComponentType elementType = (ComponentType) persister.getElementType();
        String[] propertyNames = elementType.getPropertyNames();
        Type[] subtypes = elementType.getSubtypes();
        String[] propertyParts = subAttributeName.split("\\.");
        int offset = 0;
        for (int j = 0; j < propertyParts.length - 1; j++) {
            String propertyName = propertyParts[j];

            for (int i = 0; i < propertyNames.length; i++) {
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (propertyName.equals(propertyNames[i])) {
                    if (subtypes[i] instanceof ComponentType) {
                        elementType = (ComponentType) subtypes[i];
                        propertyNames = elementType.getPropertyNames();
                        subtypes = elementType.getSubtypes();
                        break;
                    } else {
                        columnNames = new String[span];
                        String[] elementColumnNames = persister.getElementColumnNames();
                        System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                        break;
                    }
                } else {
                    offset += span;
                }
            }
        }

        if (columnNames == null) {
            String propertyName = propertyParts[propertyParts.length - 1];
            for (int i = 0; i < propertyNames.length; i++) {
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (propertyName.equals(propertyNames[i])) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    break;
                } else {
                    offset += span;
                }
            }
        }
    } else if (persister.getElementType() instanceof org.hibernate.type.EntityType) {
        Type identifierType = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyType(persister.getFactory());
        String identifierOrUniqueKeyPropertyName = ((org.hibernate.type.EntityType) persister.getElementType())
                .getIdentifierOrUniqueKeyPropertyName(persister.getFactory());
        String prefix;
        if (identifierType instanceof EmbeddedComponentType) {
            String[] propertyNames = ((EmbeddedComponentType) identifierType).getPropertyNames();
            Type[] subtypes = ((EmbeddedComponentType) identifierType).getSubtypes();
            int offset = 0;
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (subAttributeName.equals(propertyName)) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    propertyType = subtypes[i];
                    break;
                } else {
                    offset += span;
                }
            }
        } else if (subAttributeName.equals(identifierOrUniqueKeyPropertyName)) {
            columnNames = persister.getElementColumnNames();
            propertyType = identifierType;
        } else if (identifierType instanceof ComponentType
                && subAttributeName.startsWith(prefix = identifierOrUniqueKeyPropertyName + ".")) {
            String[] propertyNames = ((ComponentType) identifierType).getPropertyNames();
            Type[] subtypes = ((ComponentType) identifierType).getSubtypes();
            String subPropertyName = subAttributeName.substring(prefix.length());
            int offset = 0;
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                int span = subtypes[i].getColumnSpan(persister.getFactory());
                if (subPropertyName.equals(propertyName)) {
                    columnNames = new String[span];
                    String[] elementColumnNames = persister.getElementColumnNames();
                    System.arraycopy(elementColumnNames, offset, columnNames, 0, span);
                    propertyType = subtypes[i];
                    break;
                } else {
                    offset += span;
                }
            }
        }
    }

    if (columnNames == null) {
        throw new IllegalArgumentException(
                "Couldn't find column names for " + getTypeName(ownerType) + "#" + attributeName);
    }
    boolean isFormula = columnNames.length == 1 && columnNames[0] == null;

    if (isFormula) {
        return getColumnTypeForPropertyType(ownerType, attributeName, sfi, propertyType);
    }

    Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService();
    Table[] tables = new Table[] { database.getTable(unquote(persister.getTableName())) };

    return getColumnTypesForColumnNames(ownerType, columnNames, tables);
}

From source file:kr.debop4j.data.mongodb.test.loading.LoadSelectedColumnsCollectionTest.java

License:Apache License

protected Service getService(Class<? extends Service> serviceImpl) {
    SessionFactoryImplementor factory = super.sfi();
    ServiceRegistryImplementor serviceRegistry = factory.getServiceRegistry();
    return serviceRegistry.getService(serviceImpl);
}

From source file:kr.debop4j.data.ogm.spring.GridDatastoreConfigBase.java

License:Apache License

/**
 * hibernate {@link ServiceRegistryImplementor}   .
 *
 * @param serviceImpl  ? /*w  ww  .  ja v  a 2  s  .  co  m*/
 * @return {@link ServiceRegistryImplementor}? ?? 
 */
protected final org.hibernate.service.Service getService(
        Class<? extends org.hibernate.service.Service> serviceImpl) {
    if (log.isDebugEnabled())
        log.debug("Service  . serviceImpl=[{}]", serviceImpl.getName());

    SessionFactoryImplementor sessionFactory = getSessionFactoryImplementor();
    ServiceRegistryImplementor serviceRegistry = sessionFactory.getServiceRegistry();
    return serviceRegistry.getService(serviceImpl);
}

From source file:org.beangle.orm.hibernate.internal.SessionUtils.java

License:Open Source License

public static DataSource getDataSource(SessionFactory factory) {
    SessionFactoryImplementor factoryImpl = (SessionFactoryImplementor) factory;
    if (MultiTenancyStrategy.NONE == factoryImpl.getSettings().getMultiTenancyStrategy()) {
        return factoryImpl.getServiceRegistry().getService(ConnectionProvider.class).unwrap(DataSource.class);
    } else {/*from w  w  w.  ja  va 2s  .c  o  m*/
        return factoryImpl.getServiceRegistry().getService(MultiTenantConnectionProvider.class)
                .unwrap(DataSource.class);
    }
}

From source file:org.jadira.usertype.spi.shared.AbstractUserTypeHibernateIntegrator.java

License:Apache License

private boolean use42Api(String jdbc42Apis, SessionFactoryImplementor sessionFactory) {

    boolean use42Api;
    if (jdbc42Apis == null) {

        if (JavaVersion.getMajorVersion() >= 1 && JavaVersion.getMinorVersion() >= 8) {

            Connection conn = null;
            try {
                JdbcServices jdbcServices = sessionFactory.getServiceRegistry().getService(JdbcServices.class);
                conn = jdbcServices.getBootstrapJdbcConnectionAccess().obtainConnection();

                DatabaseMetaData dmd = conn.getMetaData();
                int driverMajorVersion = dmd.getDriverMajorVersion();
                int driverMinorVersion = dmd.getDriverMinorVersion();

                if (driverMajorVersion >= 5) {
                    use42Api = true;//from  w  w w  .ja v  a 2 s. co m
                } else if (driverMajorVersion >= 4 && driverMinorVersion >= 2) {
                    use42Api = true;
                } else {
                    use42Api = false;
                }
            } catch (SQLException e) {
                use42Api = false;
            } catch (NoSuchMethodError e) {
                // Occurs in Hibernate 4.2.12
                use42Api = false;
            } finally {

                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        // Ignore
                    }
                }
            }
        } else {
            use42Api = false;
        }
    } else {
        use42Api = Boolean.parseBoolean(jdbc42Apis);
    }
    return use42Api;
}

From source file:org.springframework.orm.hibernate4.fix.SpringSessionContext.java

License:Apache License

/**
 * Create a new SpringSessionContext for the given Hibernate SessionFactory.
 * @param sessionFactory the SessionFactory to provide current Sessions for
 *//*from   w w  w  .j ava  2  s .c  o  m*/
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
    TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    this.jtaSessionContext = (transactionManager != null ? new SpringJtaSessionContext(sessionFactory) : null);
}

From source file:org.springframework.orm.hibernate4.SpringSessionContext.java

License:Apache License

/**
 * Create a new SpringSessionContext for the given Hibernate SessionFactory.
 * @param sessionFactory the SessionFactory to provide current Sessions for
 *//*from  w ww . ja v  a  2  s  .  c  o m*/
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    try {
        Object jtaPlatform = sessionFactory.getServiceRegistry()
                .getService(ConfigurableJtaPlatform.jtaPlatformClass);
        Method rtmMethod = ConfigurableJtaPlatform.jtaPlatformClass.getMethod("retrieveTransactionManager");
        this.transactionManager = (TransactionManager) ReflectionUtils.invokeMethod(rtmMethod, jtaPlatform);
        if (this.transactionManager != null) {
            this.jtaSessionContext = new SpringJtaSessionContext(sessionFactory);
        }
    } catch (Exception ex) {
        LogFactory.getLog(SpringSessionContext.class)
                .warn("Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext", ex);
    }
}

From source file:org.springframework.orm.hibernate5.SpringSessionContext.java

License:Apache License

/**
 * Create a new SpringSessionContext for the given Hibernate SessionFactory.
 * @param sessionFactory the SessionFactory to provide current Sessions for
 *///from w  ww.ja v a2 s.  c o m
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    try {
        JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
        this.transactionManager = jtaPlatform.retrieveTransactionManager();
        if (this.transactionManager != null) {
            this.jtaSessionContext = new SpringJtaSessionContext(sessionFactory);
        }
    } catch (Exception ex) {
        LogFactory.getLog(SpringSessionContext.class)
                .warn("Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext", ex);
    }
}

From source file:sql.support.ConsumerContextImpl.java

License:LGPL

public ConsumerContextImpl(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    this.classLoaderService = sessionFactory.getServiceRegistry().getService(ClassLoaderService.class);
    this.domainMetamodel = new DomainMetamodelImpl(sessionFactory);
}