Example usage for org.hibernate.persister.entity EntityPersister getIdentifierPropertyName

List of usage examples for org.hibernate.persister.entity EntityPersister getIdentifierPropertyName

Introduction

In this page you can find the example usage for org.hibernate.persister.entity EntityPersister getIdentifierPropertyName.

Prototype

String getIdentifierPropertyName();

Source Link

Document

Get the name of the identifier property (or return null) - need not return the name of an actual Java property

Usage

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) {/*from  w  w w.ja  va 2s .  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  a  v a 2 s .c o  m
    //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);
    }
}