Search in sources :

Example 1 with Metamodel

use of javax.persistence.metamodel.Metamodel in project querydsl by querydsl.

the class AntJPADomainExporter method execute.

/**
     * Exports the named persistence unit's metamodel to Querydsl query types.  Expects to be
     * called by Ant via name convention using a method with signature public void execute().
     */
public void execute() {
    // We can assume we have the named persistence unit and its mapping file in our classpath,
    // but we may have to allow some properties in that persistence unit to be overridden before
    // we can successfully get that persistence unit's metamodel.
    Map<String, String> properties = (configuration != null) ? configuration.getProperties() : null;
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
    // Now we can get the persistence unit's metamodel and export it to Querydsl query types.
    Metamodel configuration = emf.getMetamodel();
    JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration);
    try {
        exporter.execute();
        generatedFiles = exporter.getGeneratedFiles();
    } catch (IOException e) {
        throw new RuntimeException("Error in JPADomainExporter", e);
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) IOException(java.io.IOException) Metamodel(javax.persistence.metamodel.Metamodel) File(java.io.File) JPADomainExporter(com.querydsl.jpa.codegen.JPADomainExporter)

Example 2 with Metamodel

use of javax.persistence.metamodel.Metamodel 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)

Example 3 with Metamodel

use of javax.persistence.metamodel.Metamodel in project webpieces by deanhiller.

the class HibernateLookup method find.

@SuppressWarnings("unchecked")
@Override
public <T> T find(Meta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate) {
    if (!(paramMeta instanceof ParamMeta))
        throw new UnsupportedOperationException("this plugin does not support type=" + paramMeta.getClass());
    ParamMeta m = (ParamMeta) paramMeta;
    Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
    EntityManager entityManager = Em.get();
    Metamodel metamodel = entityManager.getMetamodel();
    ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
    EntityTypeImpl<T> entityType = (EntityTypeImpl<T>) managedType;
    Class<?> idClazz = entityType.getIdType().getJavaType();
    SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
    String name = idAttribute.getName();
    ParamNode paramNode = tree.get(name);
    String value = null;
    if (paramNode != null) {
        if (!(paramNode instanceof ValueNode))
            throw new IllegalStateException("The id field in the hibernate entity should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ".  bad multipart form?  (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
        ValueNode node = (ValueNode) paramNode;
        value = node.getValue();
    }
    if (value == null)
        return beanCreate.apply(paramTypeToCreate);
    @SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
    Object id = unmarshaller.stringToObject(value);
    UseQuery namedQuery = fetchUseQuery(m.getAnnotations());
    if (namedQuery == null)
        return entityManager.find(paramTypeToCreate, id);
    Query query = entityManager.createNamedQuery(namedQuery.value());
    query.setParameter(namedQuery.id(), id);
    return (T) query.getSingleResult();
}
Also used : Query(javax.persistence.Query) ParamNode(org.webpieces.router.impl.params.ParamNode) EntityManager(javax.persistence.EntityManager) ParamMeta(org.webpieces.router.impl.params.ParamMeta) EntityTypeImpl(org.hibernate.metamodel.internal.EntityTypeImpl) ValueNode(org.webpieces.router.impl.params.ValueNode) Metamodel(javax.persistence.metamodel.Metamodel) ObjectStringConverter(org.webpieces.router.api.ObjectStringConverter)

Aggregations

Metamodel (javax.persistence.metamodel.Metamodel)3 EntityManager (javax.persistence.EntityManager)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Query (javax.persistence.Query)2 JPADomainExporter (com.querydsl.jpa.codegen.JPADomainExporter)1 File (java.io.File)1 IOException (java.io.IOException)1 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 EntityType (javax.persistence.metamodel.EntityType)1 CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)1 EntityTypeImpl (org.hibernate.metamodel.internal.EntityTypeImpl)1 After (org.junit.After)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 ObjectStringConverter (org.webpieces.router.api.ObjectStringConverter)1 ParamMeta (org.webpieces.router.impl.params.ParamMeta)1 ParamNode (org.webpieces.router.impl.params.ParamNode)1