Search in sources :

Example 1 with TypedQuery

use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.

the class TestBeforeCommitTxListener method queryWithFlush.

private void queryWithFlush(Collection<Entity> managedEntities, EntityManager entityManager) {
    if (!managedEntities.stream().anyMatch(e -> e instanceof User && ((User) e).getLogin().startsWith("TxLstnrTst-")))
        return;
    TypedQuery<User> query = entityManager.createQuery("select u from sec$User u where u.login like ?1", User.class);
    query.setParameter(1, "TxLstnrTst-2-%");
    query.setFlushMode(FlushModeType.AUTO);
    User result = query.getFirstResult();
    assertNotNull(result);
}
Also used : TestSupport(com.haulmont.cuba.testsupport.TestSupport) EntityManager(com.haulmont.cuba.core.EntityManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) FlushModeType(javax.persistence.FlushModeType) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) Group(com.haulmont.cuba.security.entity.Group) TypedQuery(com.haulmont.cuba.core.TypedQuery) Component(org.springframework.stereotype.Component) PersistenceHelper(com.haulmont.cuba.core.global.PersistenceHelper) BeforeCommitTransactionListener(com.haulmont.cuba.core.listener.BeforeCommitTransactionListener) Assert.assertEquals(org.junit.Assert.assertEquals) Entity(com.haulmont.cuba.core.entity.Entity) User(com.haulmont.cuba.security.entity.User)

Example 2 with TypedQuery

use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.

the class EntityCacheTestClass method testQuery.

@Test
public void testQuery() throws Exception {
    appender.clearMessages();
    User u1, u2;
    List<User> list;
    try (Transaction tx = cont.persistence().createTransaction()) {
        TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login like ?1", User.class);
        query.setParameter(1, "ECTest%");
        query.setViewName("user.browse");
        list = query.getResultList();
        tx.commit();
    }
    u1 = list.stream().filter(u -> u.getId().equals(this.user.getId())).findFirst().get();
    assertEquals(this.user.getLogin(), u1.getLogin());
    assertEquals(this.group, u1.getGroup());
    u2 = list.stream().filter(user -> user.getId().equals(this.user2.getId())).findFirst().get();
    assertEquals(this.user2.getLogin(), u2.getLogin());
    assertEquals(this.group, u2.getGroup());
    // User, Group
    assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = cont.persistence().createTransaction()) {
        TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login like ?1", User.class);
        query.setParameter(1, "ECTest%");
        query.setViewName("user.browse");
        list = query.getResultList();
        tx.commit();
    }
    u1 = list.stream().filter(u -> u.getId().equals(this.user.getId())).findFirst().get();
    assertEquals(this.user.getLogin(), u1.getLogin());
    assertEquals(this.group, u1.getGroup());
    u2 = list.stream().filter(user -> user.getId().equals(this.user2.getId())).findFirst().get();
    assertEquals(this.user2.getLogin(), u2.getLogin());
    assertEquals(this.group, u2.getGroup());
    // User only
    assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
Also used : Query(com.haulmont.cuba.core.Query) Arrays(java.util.Arrays) TestRule(org.junit.rules.TestRule) LoggerFactory(org.slf4j.LoggerFactory) TransactionParams(com.haulmont.cuba.core.TransactionParams) LoggerContext(ch.qos.logback.classic.LoggerContext) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) TypedQuery(com.haulmont.cuba.core.TypedQuery) EntityManagerFactoryDelegate(org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate) QueryImpl(com.haulmont.cuba.core.sys.QueryImpl) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) com.haulmont.cuba.security.entity(com.haulmont.cuba.security.entity) TestAppender(com.haulmont.cuba.testsupport.TestAppender) TestSupport.assertFail(com.haulmont.cuba.testsupport.TestSupport.assertFail) List(java.util.List) Logger(ch.qos.logback.classic.Logger) EntityManagerFactory(javax.persistence.EntityManagerFactory) AppContext(com.haulmont.cuba.core.sys.AppContext) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Transaction(com.haulmont.cuba.core.Transaction) JpaCache(org.eclipse.persistence.jpa.JpaCache) org.junit(org.junit) TestNamePrinter(com.haulmont.cuba.testsupport.TestNamePrinter) Assert(org.junit.Assert) TestSupport.reserialize(com.haulmont.cuba.testsupport.TestSupport.reserialize) TestContainer(com.haulmont.cuba.testsupport.TestContainer) Transaction(com.haulmont.cuba.core.Transaction)

Example 3 with TypedQuery

use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.

the class DynamicAttributesManager method loadAttributeValues.

protected List<CategoryAttributeValue> loadAttributeValues(MetaClass metaClass, List<Object> entityIds) {
    List<CategoryAttributeValue> attributeValues = new ArrayList<>();
    try (Transaction tx = persistence.getTransaction()) {
        EntityManager em = persistence.getEntityManager();
        View view = new View(viewRepository.getView(CategoryAttributeValue.class, View.LOCAL), null, false).addProperty("categoryAttribute", new View(viewRepository.getView(CategoryAttribute.class, View.LOCAL), null, false).addProperty("category").addProperty("defaultEntity", viewRepository.getView(ReferenceToEntity.class, View.LOCAL)));
        TypedQuery<CategoryAttributeValue> query;
        if (HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
            query = em.createQuery(format("select cav from sys$CategoryAttributeValue cav where cav.entity.%s in :ids and cav.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class);
        } else {
            query = em.createQuery(format("select cav from sys$CategoryAttributeValue cav where cav.entity.%s in :ids " + "and cav.categoryAttribute.categoryEntityType = :entityType and cav.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class);
            query.setParameter("entityType", metaClass.getName());
        }
        query.setParameter("ids", entityIds);
        query.setView(view);
        List<CategoryAttributeValue> resultList = query.getResultList();
        List<CategoryAttributeValue> cavsOfEntityType = resultList.stream().filter(cav -> cav.getObjectEntityValueId() != null).collect(Collectors.toList());
        List<CategoryAttributeValue> cavsOfCollectionType = resultList.stream().filter(cav -> cav.getCategoryAttribute().getIsCollection()).collect(Collectors.toList());
        if (cavsOfCollectionType.isEmpty()) {
            loadEntityValues(cavsOfEntityType);
            attributeValues.addAll(resultList);
        } else {
            List<CategoryAttributeValue> cavsOfCollectionTypeWithChildren = reloadCategoryAttributeValuesWithChildren(cavsOfCollectionType);
            // add nested collection values to the cavsOfEntityType collection, because this collection will later be
            // used for loading entity values
            cavsOfCollectionTypeWithChildren.stream().filter(cav -> cav.getCategoryAttribute().getDataType() == PropertyType.ENTITY && cav.getChildValues() != null).forEach(cav -> cavsOfEntityType.addAll(cav.getChildValues()));
            loadEntityValues(cavsOfEntityType);
            cavsOfCollectionTypeWithChildren.stream().filter(cav -> cav.getChildValues() != null).forEach(cav -> {
                List<Object> value = cav.getChildValues().stream().map(CategoryAttributeValue::getValue).collect(Collectors.toList());
                cav.setTransientCollectionValue(value);
            });
            attributeValues.addAll(resultList.stream().filter(cav -> !cavsOfCollectionTypeWithChildren.contains(cav)).collect(Collectors.toList()));
            attributeValues.addAll(cavsOfCollectionTypeWithChildren);
        }
        tx.commit();
    }
    return attributeValues;
}
Also used : java.util(java.util) ClusterListenerAdapter(com.haulmont.cuba.core.app.ClusterListenerAdapter) Persistence(com.haulmont.cuba.core.Persistence) LoggerFactory(org.slf4j.LoggerFactory) Multimap(com.google.common.collect.Multimap) MetaClass(com.haulmont.chile.core.model.MetaClass) CollectionUtils(org.apache.commons.collections4.CollectionUtils) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) BooleanUtils(org.apache.commons.lang.BooleanUtils) Inject(javax.inject.Inject) TypedQuery(com.haulmont.cuba.core.TypedQuery) HashMultimap(com.google.common.collect.HashMultimap) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) EntityManager(com.haulmont.cuba.core.EntityManager) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Serializable(java.io.Serializable) ClusterManagerAPI(com.haulmont.cuba.core.app.ClusterManagerAPI) Component(org.springframework.stereotype.Component) Transaction(com.haulmont.cuba.core.Transaction) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Aggregations

TypedQuery (com.haulmont.cuba.core.TypedQuery)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 Inject (javax.inject.Inject)2 LoggerFactory (org.slf4j.LoggerFactory)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 Persistence (com.haulmont.cuba.core.Persistence)1 Query (com.haulmont.cuba.core.Query)1 TransactionParams (com.haulmont.cuba.core.TransactionParams)1 ClusterListenerAdapter (com.haulmont.cuba.core.app.ClusterListenerAdapter)1 ClusterManagerAPI (com.haulmont.cuba.core.app.ClusterManagerAPI)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Metadata (com.haulmont.cuba.core.global.Metadata)1