Search in sources :

Example 1 with EntityType

use of javax.persistence.metamodel.EntityType in project hibernate-orm by hibernate.

the class PluralAttributePath method locateNearestSubclassEntity.

private EntityType locateNearestSubclassEntity(MappedSuperclassType mappedSuperclassType, EntityType entityTypeTop) {
    EntityType entityTypeNearestDeclaringType = entityTypeTop;
    IdentifiableType superType = entityTypeNearestDeclaringType.getSupertype();
    while (superType != mappedSuperclassType) {
        if (superType == null) {
            throw new IllegalStateException(String.format("Cannot determine nearest EntityType extending mapped superclass [%s] starting from [%s]; a supertype of [%s] is null", mappedSuperclassType.getJavaType().getName(), entityTypeTop.getJavaType().getName(), entityTypeTop.getJavaType().getName()));
        }
        if (superType.getPersistenceType() == Type.PersistenceType.ENTITY) {
            entityTypeNearestDeclaringType = (EntityType) superType;
        }
        superType = superType.getSupertype();
    }
    return entityTypeNearestDeclaringType;
}
Also used : EntityType(javax.persistence.metamodel.EntityType) IdentifiableType(javax.persistence.metamodel.IdentifiableType)

Example 2 with EntityType

use of javax.persistence.metamodel.EntityType in project hibernate-orm by hibernate.

the class MetamodelImpl method applyNamedEntityGraphs.

@SuppressWarnings("unchecked")
private void applyNamedEntityGraphs(java.util.Collection<NamedEntityGraphDefinition> namedEntityGraphs) {
    for (NamedEntityGraphDefinition definition : namedEntityGraphs) {
        log.debugf("Applying named entity graph [name=%s, entity-name=%s, jpa-entity-name=%s", definition.getRegisteredName(), definition.getEntityName(), definition.getJpaEntityName());
        final EntityType entityType = entity(definition.getEntityName());
        if (entityType == null) {
            throw new IllegalArgumentException("Attempted to register named entity graph [" + definition.getRegisteredName() + "] for unknown entity [" + definition.getEntityName() + "]");
        }
        final EntityGraphImpl entityGraph = new EntityGraphImpl(definition.getRegisteredName(), entityType, this.getSessionFactory());
        final NamedEntityGraph namedEntityGraph = definition.getAnnotation();
        if (namedEntityGraph.includeAllAttributes()) {
            for (Object attributeObject : entityType.getAttributes()) {
                entityGraph.addAttributeNodes((Attribute) attributeObject);
            }
        }
        if (namedEntityGraph.attributeNodes() != null) {
            applyNamedAttributeNodes(namedEntityGraph.attributeNodes(), namedEntityGraph, entityGraph);
        }
        entityGraphMap.put(definition.getRegisteredName(), entityGraph);
    }
}
Also used : NamedEntityGraphDefinition(org.hibernate.cfg.annotations.NamedEntityGraphDefinition) EntityType(javax.persistence.metamodel.EntityType) EntityGraphImpl(org.hibernate.jpa.graph.internal.EntityGraphImpl) NamedEntityGraph(javax.persistence.NamedEntityGraph)

Example 3 with EntityType

use of javax.persistence.metamodel.EntityType in project uPortal by Jasig.

the class BaseJpaDaoTest method deleteAllEntities.

/** Deletes ALL entities from the database */
@After
public final void deleteAllEntities() {
    final EntityManager entityManager = getEntityManager();
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final Metamodel metamodel = entityManagerFactory.getMetamodel();
    Set<EntityType<?>> entityTypes = new LinkedHashSet<EntityType<?>>(metamodel.getEntities());
    do {
        final Set<EntityType<?>> failedEntitieTypes = new HashSet<EntityType<?>>();
        for (final EntityType<?> entityType : entityTypes) {
            final String entityClassName = entityType.getBindableJavaType().getName();
            try {
                this.executeInTransaction(new CallableWithoutResult() {

                    @Override
                    protected void callWithoutResult() {
                        logger.trace("Purging all: " + entityClassName);
                        final Query query = entityManager.createQuery("SELECT e FROM " + entityClassName + " AS e");
                        final List<?> entities = query.getResultList();
                        logger.trace("Found " + entities.size() + " " + entityClassName + " to delete");
                        for (final Object entity : entities) {
                            entityManager.remove(entity);
                        }
                    }
                });
            } catch (DataIntegrityViolationException e) {
                logger.trace("Failed to delete " + entityClassName + ". Must be a dependency of another entity");
                failedEntitieTypes.add(entityType);
            }
        }
        entityTypes = failedEntitieTypes;
    } while (!entityTypes.isEmpty());
    //Reset all spring managed mocks after every test
    MockitoFactoryBean.resetAllMocks();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Query(javax.persistence.Query) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) EntityType(javax.persistence.metamodel.EntityType) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) LinkedList(java.util.LinkedList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) Metamodel(javax.persistence.metamodel.Metamodel) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) After(org.junit.After)

Aggregations

EntityType (javax.persistence.metamodel.EntityType)3 AccessibleObject (java.lang.reflect.AccessibleObject)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 NamedEntityGraph (javax.persistence.NamedEntityGraph)1 Query (javax.persistence.Query)1 IdentifiableType (javax.persistence.metamodel.IdentifiableType)1 Metamodel (javax.persistence.metamodel.Metamodel)1 CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)1 NamedEntityGraphDefinition (org.hibernate.cfg.annotations.NamedEntityGraphDefinition)1 EntityGraphImpl (org.hibernate.jpa.graph.internal.EntityGraphImpl)1 After (org.junit.After)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1