Search in sources :

Example 6 with Query

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

the class UniqueNumbers method checkSequenceExists.

protected void checkSequenceExists(String domain) {
    String seqName = getSequenceName(domain);
    if (containsSequence(seqName))
        return;
    // Create sequence in separate transaction because it's name is cached and we want to be sure it is created
    // regardless of possible errors in the invoking code
    Transaction tx = persistence.createTransaction(getDataStore(domain));
    try {
        lock.readLock().unlock();
        lock.writeLock().lock();
        EntityManager em = persistence.getEntityManager(getDataStore(domain));
        Query query = em.createNativeQuery(getSequenceSupport(domain).sequenceExistsSql(seqName));
        List list = query.getResultList();
        if (list.isEmpty()) {
            query = em.createNativeQuery(getSequenceSupport(domain).createSequenceSql(seqName, 1, 1));
            query.executeUpdate();
        }
        tx.commit();
        existingSequences.add(seqName);
    } finally {
        lock.readLock().lock();
        lock.writeLock().unlock();
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List)

Example 7 with Query

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

the class DeletePolicyProcessor method getReference.

protected Entity getReference(Entity entity, MetaProperty property) {
    if (PersistenceHelper.isLoaded(entity, property.getName()))
        return entity.getValue(property.getName());
    else {
        Query query = entityManager.createQuery("select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1");
        query.setParameter(1, entity.getId());
        Object refEntity = query.getFirstResult();
        return (Entity) refEntity;
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query)

Example 8 with Query

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

the class DeletePolicyProcessor method cascade.

protected void cascade(String entityName, MetaProperty property) {
    String template = property.getRange().getCardinality().isMany() ? "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" : "select e from %s e where e.%s." + primaryKeyName + " = ?1";
    String qstr = String.format(template, entityName, property.getName());
    Query query = entityManager.createQuery(qstr);
    query.setParameter(1, entity.getId());
    List<Entity> list = query.getResultList();
    for (Entity e : list) {
        entityManager.remove(e);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query)

Example 9 with Query

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

the class DeletePolicyProcessor method isCollectionEmpty.

protected boolean isCollectionEmpty(MetaProperty property) {
    MetaProperty inverseProperty = property.getInverse();
    if (inverseProperty == null) {
        log.warn("Inverse property not found for property {}", property);
        Collection<Entity> value = entity.getValue(property.getName());
        return value == null || value.isEmpty();
    }
    String invPropName = inverseProperty.getName();
    String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());
    String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." + primaryKeyName + " = ?1";
    Query query = entityManager.createQuery(qlStr);
    query.setParameter(1, entity.getId());
    query.setMaxResults(1);
    List<Entity> list = query.getResultList();
    return list.isEmpty();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 10 with Query

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

the class SchedulingServiceBean method getAvailableUsers.

@Override
public List<String> getAvailableUsers() {
    List<String> result = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        Query query = em.createQuery("select u from sec$User u");
        List<User> userList = query.getResultList();
        for (User user : userList) {
            result.add(user.getLogin());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return result;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) ArrayList(java.util.ArrayList)

Aggregations

Query (com.haulmont.cuba.core.Query)42 EntityManager (com.haulmont.cuba.core.EntityManager)27 Transaction (com.haulmont.cuba.core.Transaction)25 TypedQuery (com.haulmont.cuba.core.TypedQuery)12 Entity (com.haulmont.cuba.core.entity.Entity)7 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Date (java.util.Date)3 TransactionParams (com.haulmont.cuba.core.TransactionParams)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 User (com.haulmont.cuba.security.entity.User)2 After (org.junit.After)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 StringHelper (com.haulmont.bali.util.StringHelper)1 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 PersistenceSecurity (com.haulmont.cuba.core.PersistenceSecurity)1