Example usage for javax.persistence Query executeUpdate

List of usage examples for javax.persistence Query executeUpdate

Introduction

In this page you can find the example usage for javax.persistence Query executeUpdate.

Prototype

int executeUpdate();

Source Link

Document

Execute an update or delete statement.

Usage

From source file:org.yestech.lib.jpa.YesJpaTemplate.java

public int bulkUpdateNamedQuery(final String queryName, final Object... values) throws DataAccessException {

    return (Integer) execute(new JpaCallback() {
        @Override//  ww  w  .j  av  a 2 s.  co m
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query queryObject = em.createNamedQuery(queryName);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    queryObject.setParameter(i, values[i]);
                }
            }
            return queryObject.executeUpdate();
        }
    });

}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

public boolean deleteAcademicTerm(Collection<AcademicTerm> academicList) {
    Query query = em.createQuery("UPDATE AcademicTerm a SET isDeleted = true WHERE a in ?1");
    query.setParameter(1, academicList);
    int n = query.executeUpdate();
    if (n > 0)
        return true;
    return false;
}

From source file:com.bitplan.vzjava.TestVZJPA.java

/**
 * test importing power VAlues in XML Format
 * /* ww  w .  j av  a2  s.c om*/
 * @throws Exception
 */
// https://github.com/WolfgangFahl/com.bitplan.vzjava/issues/5
@Test
public void testImportXml() throws Exception {
    // open the testdatabase
    VZDB vzdb = new VZDB("demo");
    // get the power values from the XML file
    File powerValueXmlFile = new File("src/test/data/vzdb/powervalues.xml");
    String xml = FileUtils.readFileToString(powerValueXmlFile);
    PowerValueManagerDao pvm = (PowerValueManagerDao) PowerValueManagerDao.getFactoryStatic().fromXML(xml);
    List<PowerValue> powerValues = pvm.getElements();
    // there should be 74669 power values in this test set
    assertEquals("xml import should have new Color(0x of records", 74669, powerValues.size());
    // delete existing data from the test database
    EntityManager em = vzdb.getEntityManager();
    em.getTransaction().begin();
    Query dquery = em.createNativeQuery("delete from data");
    dquery.executeUpdate();
    em.getTransaction().commit();

    String from = "2017-01-31 20:00:00";
    String to = "2017-03-24 14:00:00";
    em.getTransaction().begin();
    for (PowerValue powerValue : powerValues) {
        em.persist(powerValue);
    }
    em.getTransaction().commit();
    int channel = 4;
    ChannelMode channelMode = ChannelMode.Power;
    pvm.setVzdb(vzdb);
    List<PowerValue> dbPowerValues = pvm.get(from, to, channel, channelMode);
    assertTrue(String.format("database should have more than 74400 of imported records but has %5d",
            dbPowerValues.size()), dbPowerValues.size() > 74400);
}

From source file:com.headissue.pigeon.service.AdminSurveyService.java

void mergeQuestion(Survey _survey, SurveyValue _value, EntityManager _manager) {
    List<Question> _items = new ArrayList<Question>(_survey.getQuestions());
    int _orderBy = 0;
    Question q = null;//from ww  w . j a va 2 s  .c om
    for (QuestionValue qv : _value.getQuestions()) {
        if (!_items.isEmpty()) {
            q = beanService.copy(_items.remove(0), qv, QUESTION_PROPERTIES);
            q.setOrderBy(++_orderBy);
            LogUtils.debug(log, "recycle question %s: '%s'", q.getId(), q.getText());
        } else {
            q = beanService.copy(Question.class, qv, QUESTION_PROPERTIES);
            _survey.addQuestion(q, ++_orderBy);
            LogUtils.debug(log, "add a new question:  '%s'", q.getText());
        }
        mergeQuestionText(q, qv, _manager);
    }
    if (!_items.isEmpty()) {
        for (Question qq : _items) {
            LogUtils.debug(log, "remove question %s", qq.getId());
            Query query = _manager.createNamedQuery("answer.deleteAnswerByQuestion");
            query.setParameter("question", qq);
            query.executeUpdate();
            _survey.getQuestions().remove(qq);
        }
    }
}

From source file:com.music.dao.UserDao.java

public void deleteSocialAuthentication(Long userId, String providerId) {
    Query query = getEntityManager()
            .createQuery("DELETE FROM SocialAuthentication WHERE user.id=:userId AND providerId=:providerId");
    query.setParameter("userId", userId);
    query.setParameter("providerId", providerId);
    query.executeUpdate();
}

From source file:es.ucm.fdi.dalgs.group.repository.GroupRepository.java

public boolean deleteGroupsFromCourses(Collection<Course> coursesList) {
    try {/*from   w  w  w . j  a v a 2s .c  o m*/
        Query query = em.createQuery("UPDATE Group g SET g.isDeleted = true where g.course in ?1");

        query.setParameter(1, coursesList);
        query.executeUpdate();

    } catch (Exception e) {
        logger.error(e.getMessage());

        return false;
    }
    return true;
}

From source file:org.orcid.persistence.dao.impl.ProfileKeywordDaoImpl.java

/**
 * Deleted a keyword from database//  w  ww  .  jav  a2 s  . c o m
 * @param orcid
 * @param keyword
 * @return true if the keyword was successfully deleted
 * */
@Override
@Transactional
public boolean deleteProfileKeyword(String orcid, String keyword) {
    Query query = entityManager
            .createQuery("DELETE FROM ProfileKeywordEntity WHERE profile.id = :orcid AND keyword = :keyword");
    query.setParameter("orcid", orcid);
    query.setParameter("keyword", keyword);
    return query.executeUpdate() > 0 ? true : false;
}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

public boolean modifyTerm(String term, String newTerm) {
    Query query = em.createQuery("UPDATE AcademicTerm SET term =?1 WHERE term=?2");
    query.setParameter(1, newTerm);//w w  w  . j a  v a2 s .c o  m
    query.setParameter(2, term);

    query.executeUpdate();
    if (query.executeUpdate() >= 0)
        return true;
    return false;
}

From source file:me.ronghai.sa.dao.impl.AbstractModelDAOWithEMImpl.java

@Override
public int remove(boolean force, Collection<Long> ids) {
    if (ids == null || ids.isEmpty()) {
        return 0;
    }/*w  ww  .  ja v a 2s  .  co  m*/
    String sql;
    if (force) {
        sql = "DELETE FROM " + entityClass.getName() + "  e  where id in (:ids) ";
    } else {
        sql = "UPDATE " + entityClass.getName() + "  SET disabled = 1  where id in (:ids) ";
    }
    System.out.println(sql);
    Query query = entityManager.createQuery(sql);
    query.setParameter("ids", ids);
    int s = query.executeUpdate();
    return s;
}

From source file:org.apache.openmeetings.db.dao.user.PrivateMessagesDao.java

public Integer moveMailsToFolder(Collection<Long> ids, Long folderId) {
    Query query = em.createNamedQuery("moveMailsToFolder");
    query.setParameter("folderId", folderId);
    query.setParameter("ids", ids);
    return query.executeUpdate();
}