Search in sources :

Example 16 with EntityManager

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

the class TestingServiceBean method executeSelectSql.

@Override
@Transactional(timeout = 2)
public String executeSelectSql(String sql) {
    checkTestMode();
    log.info("started: " + sql);
    EntityManager em = persistence.getEntityManager();
    Query query = em.createNativeQuery(sql);
    query.getResultList();
    log.info("finished: " + sql);
    return "Done";
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Query(com.haulmont.cuba.core.Query) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with EntityManager

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

the class TestingServiceBean method executeUpdateSql.

@Override
@Transactional(timeout = 2)
public String executeUpdateSql(String sql) {
    checkTestMode();
    log.info("started: " + sql);
    EntityManager em = persistence.getEntityManager();
    Query query = em.createNativeQuery(sql);
    query.executeUpdate();
    log.info("finished: " + sql);
    return "Done";
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Query(com.haulmont.cuba.core.Query) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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 19 with EntityManager

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

the class UniqueNumbers method executeScript.

protected Object executeScript(String domain, String sqlScript) {
    EntityManager em = persistence.getEntityManager(getDataStore(domain));
    StrTokenizer tokenizer = new StrTokenizer(sqlScript, SequenceSupport.SQL_DELIMITER);
    Object value = null;
    Connection connection = em.getConnection();
    while (tokenizer.hasNext()) {
        String sql = tokenizer.nextToken();
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            try {
                if (statement.execute()) {
                    ResultSet rs = statement.getResultSet();
                    if (rs.next())
                        value = rs.getLong(1);
                }
            } finally {
                DbUtils.closeQuietly(statement);
            }
        } catch (SQLException e) {
            throw new IllegalStateException("Error executing SQL for getting next number", e);
        }
    }
    return value;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StrTokenizer(org.apache.commons.lang.text.StrTokenizer)

Example 20 with EntityManager

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

the class StandardCacheLoader method updateData.

@Override
public void updateData(CacheSet cacheSet, Map<String, Object> params) throws CacheException {
    if (configuration.getConfig(GlobalConfig.class).getPerformanceTestMode())
        return;
    Collection<Object> items = cacheSet.getItems();
    List updateItems = (List) params.get("items");
    if ((updateItems != null) && (updateItems.size() > 0)) {
        MetaClass metaClass = metadata.getSession().getClass(metaClassName);
        View view = metadata.getViewRepository().getView(metaClass, viewName);
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            for (Object item : updateItems) {
                Entity entity = (Entity) item;
                entity = em.find(entity.getClass(), entity.getId(), view);
                items.remove(item);
                if (entity != null)
                    items.add(entity);
            }
            tx.commit();
        } catch (Exception e) {
            throw new CacheException(e);
        } finally {
            tx.end();
        }
    } else {
        log.debug("Nothing to update");
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4