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

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

Introduction

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

Prototype

@Deprecated
default EntityPersister getEntityPersister(String entityName) throws MappingException 

Source Link

Usage

From source file:com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator.java

License:Apache License

/**
 * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade.
 * Method provides translation from hibernate {@link Criteria} to plain SQL string query.
 *
 * @param criteria//from w w w . j  av a2s .co  m
 * @return SQL string, null if criteria parameter was null.
 */
public static String toSql(Criteria criteria) {
    if (criteria == null) {
        return null;
    }

    try {
        CriteriaImpl c;
        if (criteria instanceof CriteriaImpl) {
            c = (CriteriaImpl) criteria;
        } else {
            CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) criteria;
            c = (CriteriaImpl) subcriteria.getParent();
        }
        SessionImpl s = (SessionImpl) c.getSession();
        SessionFactoryImplementor factory = s.getSessionFactory();
        String[] implementors = factory.getImplementors(c.getEntityOrClassName());
        CriteriaLoader loader = new CriteriaLoader(
                (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0],
                s.getLoadQueryInfluencers());
        Field f = OuterJoinLoader.class.getDeclaredField("sql");
        f.setAccessible(true);
        return (String) f.get(loader);
    } catch (Exception ex) {
        throw new SystemException(ex.getMessage(), ex);
    }
}

From source file:com.github.shyiko.rook.target.hibernate4.cache.QueryCacheSynchronizer.java

License:Apache License

private String[] resolveQuerySpaces(EvictionTarget evictionTarget) {
    String role = evictionTarget.getName();
    SessionFactoryImplementor factory = synchronizationContext.getSessionFactory();
    Serializable[] spaces;/*  w ww .  j  a v a  2 s  . c o  m*/
    if (evictionTarget.isCollection()) {
        spaces = factory.getCollectionPersister(role).getCollectionSpaces();
    } else {
        // todo(shyiko): how about querySpaces?
        spaces = factory.getEntityPersister(role).getPropertySpaces();
    }
    return spaces == null ? EMPTY_STRING_ARRAY : (String[]) spaces;
}

From source file:org.babyfish.hibernate.jpa.internal.XEntityManagerImpl.java

License:Open Source License

protected <E> E findByQueryPaths(Class<E> entityClass, Object primaryKey, LockModeType nullableLockModeType, //can be null 
        QueryPath[] queryPaths) {//  w w  w  .j  ava2  s.c  o m
    if (queryPaths == null || queryPaths.length == 0) {
        return this.find(entityClass, primaryKey, nullableLockModeType, (Map<String, Object>) null);
    }

    //This exception need not to be converted because this is a reading action so that it is not need to markAsRollback
    Arguments.mustBeInstanceOfValue("primaryKey", primaryKey, Serializable.class);

    try {
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) this.getEntityManagerFactory()
                .unwrap(SessionFactoryImplementor.class);
        EntityPersister persister = sfi.getEntityPersister(entityClass.getName());
        String idPropertyName = persister.getIdentifierPropertyName();
        XTypedQuery<E> typedQuery = this
                .createQuery(
                        "select babyfish_alias_0 from " + entityClass.getName()
                                + " babyfish_alias_0 where babyfish_alias_0." + idPropertyName + " = :id",
                        entityClass)
                .setQueryPaths(queryPaths).setParameter("id", primaryKey);
        if (nullableLockModeType != null) {
            typedQuery.setLockMode(nullableLockModeType);
        }
        return typedQuery.getSingleResult(true);
    } catch (NoResultException ex) {
        throw new EntityNotFoundException();
    } catch (MappingException ex) {
        throw new IllegalArgumentException(ex);
    } catch (HibernateException ex) {
        throw this.convert(ex);
    }
}

From source file:org.babyfish.hibernate.jpa.internal.XEntityManagerImpl.java

License:Open Source License

protected <E> List<E> findByQueryPaths(Class<E> entityClass, Iterable<?> primaryKeys,
        LockModeType nullableLockModeType, //can be null 
        QueryPath[] queryPaths) {//w ww. j av a2s . com
    //This exception need not to be converted because this is a reading action so that it is not need to markAsRollback
    Arguments.mustNotBeNull("primaryKeys", primaryKeys);

    Set<Object> primaryKeySet;
    if (primaryKeys instanceof Collection<?>) {
        primaryKeySet = new LinkedHashSet<>((((Collection<?>) primaryKeys).size() * 4 + 2) / 3);
    } else {
        primaryKeySet = new LinkedHashSet<>();
    }
    for (Object primaryKey : primaryKeys) {
        if (primaryKey != null) {
            primaryKeySet.add(primaryKey);
        }
    }
    if (primaryKeySet.isEmpty()) {
        //Let the returned list can be modified, so don't let it return MACollections.emptyList();
        return new ArrayList<>();
    }

    try {
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) this.getEntityManagerFactory()
                .unwrap(SessionFactoryImplementor.class);
        EntityPersister persister = sfi.getEntityPersister(entityClass.getName());
        String idPropertyName = persister.getIdentifierPropertyName();
        XTypedQuery<E> typedQuery;
        if (primaryKeySet.size() == 1) {
            typedQuery = this.createQuery(
                    "select babyfish_alias_0 from " + entityClass.getName()
                            + " babyfish_alias_0 where babyfish_alias_0." + idPropertyName + " = :id",
                    entityClass).setParameter("id", primaryKeySet.iterator().next());
        } else {
            typedQuery = this.createQuery(
                    "select babyfish_alias_0 from " + entityClass.getName()
                            + " babyfish_alias_0 where babyfish_alias_0." + idPropertyName + " in (:ids)",
                    entityClass).setParameter("ids", primaryKeySet);
        }
        typedQuery.setQueryPaths(queryPaths);
        if (nullableLockModeType != null) {
            typedQuery.setLockMode(nullableLockModeType);
        }
        return typedQuery.getResultList();
    } catch (MappingException ex) {
        throw new IllegalArgumentException(ex);
    } catch (HibernateException ex) {
        throw this.convert(ex);
    }
}

From source file:org.infinispan.test.hibernate.cache.CacheKeySerializationTest.java

License:LGPL

private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception {
    final SessionFactoryImplementor sessionFactory = getSessionFactory(cacheKeysFactory.getClass().getName());
    final EntityPersister persister = sessionFactory.getEntityPersister(entityName);
    final Object key = cacheKeysFactory.createEntityKey(id, persister, sessionFactory, null);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(key);/*w w w.  jav a2s. c  om*/

    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Object keyClone = ois.readObject();

    try {
        assertEquals(key, keyClone);
        assertEquals(keyClone, key);

        assertEquals(key.hashCode(), keyClone.hashCode());

        final Object idClone = cacheKeysFactory.getEntityId(keyClone);

        assertEquals(id.hashCode(), idClone.hashCode());
        assertEquals(id, idClone);
        assertEquals(idClone, id);
        assertTrue(persister.getIdentifierType().isEqual(id, idClone, sessionFactory));
        assertTrue(persister.getIdentifierType().isEqual(idClone, id, sessionFactory));
        sessionFactory.close();
    } finally {
        sessionFactory.close();
    }
}

From source file:org.jspresso.framework.model.persistence.hibernate.entity.persister.EntityProxyJoinedSubclassEntityPersister.java

License:Open Source License

/**
 * Takes a chance to determine the entity persister from the proxy handler
 * contract./*from  ww w  .  ja  v  a2s  .  co  m*/
 * <p>
 * {@inheritDoc}
 */
@Override
public EntityPersister getSubclassEntityPersister(Object instance, SessionFactoryImplementor factory) {
    if (instance instanceof IEntity) {
        return factory.getEntityPersister(((IEntity) instance).getComponentContract().getName());
    }
    return super.getSubclassEntityPersister(instance, factory);
}

From source file:org.n52.series.db.DataModelUtil.java

License:Open Source License

public static String getSqlString(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = extractSessionFactory(criteria);
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    return walker.getSQLString();
}

From source file:org.n52.sos.ds.hibernate.util.HibernateHelper.java

License:Open Source License

/**
 * Get the SQL query string from Criteria.
 *
 * @param criteria/*  www .ja va2 s .c  om*/
 *            Criteria to get SQL query string from
 * @return SQL query string from criteria
 */
public static String getSqlString(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    return walker.getSQLString();
}

From source file:support.SqlUtils.java

public static String getSql(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    String sql = walker.getSQLString();
    return sql;/*from   ww w . j av a2  s.  c  om*/
}