Example usage for javax.persistence EntityManager flush

List of usage examples for javax.persistence EntityManager flush

Introduction

In this page you can find the example usage for javax.persistence EntityManager flush.

Prototype

public void flush();

Source Link

Document

Synchronize the persistence context to the underlying database.

Usage

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testSetResetAndFlush() throws IOException {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*  w  w  w.  j av a 2s  . c  o  m*/
    LobEntity le = newLobEntity(createLobData(), 1);
    em.persist(le);
    String string = createLobData2();
    changeStream(le, string);
    em.flush();
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    assertEquals(string, getStreamContentAsString(entity.getStream()));
    em.getTransaction().commit();
    em.close();
}

From source file:nl.b3p.kaartenbalie.struts.WMSUrlCreatorAction.java

public ActionForward getMapUrl(ActionMapping mapping, DynaValidatorForm dynaForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (!isTokenValid(request)) {
        prepareMethod(dynaForm, request, EDIT, LIST);
        addAlternateMessage(mapping, request, TOKEN_ERROR_KEY);
        return getAlternateForward(mapping, request);
    }/*from www  .j  a v a 2s.c  om*/

    ActionErrors errors = dynaForm.validate(mapping, request);
    if (!errors.isEmpty()) {
        super.addMessages(request, errors);
        prepareMethod(dynaForm, request, EDIT, LIST);
        addAlternateMessage(mapping, request, VALIDATION_ERROR_KEY);
        return getAlternateForward(mapping, request);
    }

    //Create the String
    String[] layers = (String[]) dynaForm.get("sortedLayers");
    String projectie = (String) dynaForm.get("selectedProjectie");
    String bbox = (String) dynaForm.get("bbox");
    Integer height = (Integer) dynaForm.get("height");
    Integer width = (Integer) dynaForm.get("width");
    String format = (String) dynaForm.get("selectedFormat");
    String pUrl = (String) dynaForm.get("personalUrl");

    if (layers.length == 0) {
        prepareMethod(dynaForm, request, LIST, LIST);
        addAlternateMessage(mapping, request, NO_LAYERS_SELECTED_ERROR_KEY);
        return getAlternateForward(mapping, request);
    }

    /*
     * Get the URL to start with
     */
    User user = (User) request.getUserPrincipal();
    if (user == null) {
        prepareMethod(dynaForm, request, LIST, LIST);
        addAlternateMessage(mapping, request, UNKNOWN_SES_USER_ERROR_KEY);
        return getAlternateForward(mapping, request);
    }

    StringBuffer layerString = new StringBuffer();
    for (int i = 0; i < layers.length; i++) {
        layerString.append(layers[i]);
        layerString.append(",");
    }
    String layer = layerString.substring(0, layerString.lastIndexOf(","));

    StringBuffer getMapUrl = new StringBuffer(user.getPersonalURL(request));
    getMapUrl.append("?");
    getMapUrl.append(OGCConstants.WMS_SERVICE);
    getMapUrl.append("=");
    getMapUrl.append(OGCConstants.WMS_SERVICE_WMS);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_VERSION);
    getMapUrl.append("=");
    getMapUrl.append(OGCConstants.WMS_VERSION_111);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_REQUEST);
    getMapUrl.append("=");
    getMapUrl.append(OGCConstants.WMS_REQUEST_GetMap);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_LAYERS);
    getMapUrl.append("=");
    getMapUrl.append(layer);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_BBOX);
    getMapUrl.append("=");
    getMapUrl.append(bbox);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_SRS);
    getMapUrl.append("=");
    getMapUrl.append(projectie);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_HEIGHT);
    getMapUrl.append("=");
    getMapUrl.append(height);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_WIDTH);
    getMapUrl.append("=");
    getMapUrl.append(width);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_FORMAT);
    getMapUrl.append("=");
    getMapUrl.append(format);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_BGCOLOR);
    getMapUrl.append("=");
    getMapUrl.append("0xF0F0F0");
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_EXCEPTIONS);
    getMapUrl.append("=");
    getMapUrl.append(OGCConstants.WMS_PARAM_EXCEPTION_INIMAGE);
    getMapUrl.append("&");
    getMapUrl.append(OGCConstants.WMS_PARAM_STYLES);
    getMapUrl.append("=");

    user.setDefaultGetMap(getMapUrl.toString());

    log.debug("Getting entity manager ......");
    EntityManager em = getEntityManager();
    if (user.getId() == null) {
        em.persist(user);
    } else {
        em.merge(user);
    }
    em.flush();

    populateForm(getMapUrl.toString(), dynaForm, request);
    prepareMethod(dynaForm, request, LIST, EDIT);
    addDefaultMessage(mapping, request, ACKNOWLEDGE_MESSAGES);
    return getDefaultForward(mapping, request);
}

From source file:nl.b3p.kaartenbalie.struts.ReportingAction.java

public ActionForward delete(ActionMapping mapping, DynaValidatorForm dynaForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    log.debug("Getting entity manager ......");
    EntityManager em = getEntityManager();
    String[] deleteList = (String[]) dynaForm.get("deleteReport");
    for (int i = 0; i < deleteList.length; i++) {
        Integer id = FormUtils.StringToInteger(deleteList[i]);
        Report report = (Report) em.find(Report.class, id);
        if (report != null) {
            em.remove(report);// w  w w .  j  av a  2s .  co m
        }
        em.flush();
    }

    dynaForm.initialize(mapping);
    prepareMethod(dynaForm, request, LIST, EDIT);
    addDefaultMessage(mapping, request, ACKNOWLEDGE_MESSAGES);
    return getDefaultForward(mapping, request);
}

From source file:org.rhq.enterprise.server.content.test.RepoManagerBeanTest.java

@Test(enabled = ENABLED)
public void createDeleteRepoGroup() throws Exception {
    // Setup/*from   w  ww. j av  a  2s . co  m*/
    EntityManager entityManager = getEntityManager();

    RepoGroupType groupType = new RepoGroupType("testCreateDeleteRepoGroupType");
    entityManager.persist(groupType);
    entityManager.flush();

    String groupName = "testCreateDeleteRepoGroup";
    RepoGroup group = repoManager.getRepoGroupByName(groupName);
    assert group == null;

    // Test
    group = new RepoGroup(groupName);
    group.setRepoGroupType(groupType);
    group = repoManager.createRepoGroup(overlord, group);

    // Verify
    int id = group.getId();
    group = repoManager.getRepoGroup(overlord, id);
    assert group != null;
    assert group.getName().equals(groupName);

    // Cleanup
    repoManager.deleteRepoGroup(overlord, id);
    group = repoManager.getRepoGroup(overlord, id);
    assert group == null;

    entityManager.remove(groupType);
}

From source file:org.rhq.enterprise.server.content.test.RepoManagerBeanTest.java

@Test(enabled = ENABLED)
public void getRepoGroupTypeByName() throws Exception {
    // Setup/*from   w  w w. ja v  a2 s  . c  om*/
    EntityManager entityManager = getEntityManager();
    String name = "test-repo-type";

    RepoGroupType groupType = new RepoGroupType(name);
    entityManager.persist(groupType);
    entityManager.flush();

    // Test
    RepoGroupType type = repoManager.getRepoGroupTypeByName(overlord, name);
    assert type != null;
    assert type.getName().equals(name);

    // Cleanup
    type = entityManager.find(RepoGroupType.class, type.getId());
    entityManager.remove(type);
    entityManager.flush();
}

From source file:org.rhq.enterprise.server.content.test.RepoManagerBeanTest.java

@Test(enabled = ENABLED)
public void createDuplicateRepoGroup() throws Exception {
    // Setup/*from   w  ww. jav  a 2s  . c  o m*/
    EntityManager entityManager = getEntityManager();

    RepoGroupType groupType = new RepoGroupType("testCreateDuplicateRepoGroup");
    entityManager.persist(groupType);
    entityManager.flush();

    String groupName = "testCreateDuplicateRepoGroup";

    RepoGroup existing = new RepoGroup(groupName);
    existing.setRepoGroupType(groupType);
    repoManager.createRepoGroup(overlord, existing);

    existing = repoManager.getRepoGroupByName(groupName);
    assert existing != null;

    // Test
    RepoGroup duplicate = new RepoGroup(groupName);
    duplicate.setRepoGroupType(groupType);

    try {
        repoManager.createRepoGroup(overlord, existing);
        assert false;
    } catch (RepoException e) {
        // Expected
    }

    // Cleanup
    repoManager.deleteRepoGroup(overlord, existing.getId());
    existing = repoManager.getRepoGroup(overlord, existing.getId());
    assert existing == null;

    entityManager.remove(groupType);
}

From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java

/**
 * //from w w  w  .jav a2  s .  c  o  m
 * @param transactionClass
 * @param description
 * @return
 * @throws java.lang.Exception
 */
public Transaction prepareTransaction(int type, String description) throws Exception {
    if (!isEnableAccounting()) {
        return null;
    }
    log.debug("Getting entity manager ......");
    EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);
    Account account = (Account) em.find(Account.class, organizationId);

    Transaction transaction = new Transaction();
    transaction.setType(type);
    transaction.setStatus(Transaction.PENDING);
    transaction.setAccount(account);
    transaction.setDescription(description);
    em.persist(transaction);
    em.flush();

    return transaction;
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void saveCategory(Category c) {
    logger.info("Will category = " + c.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/* www  .j av a2 s . co  m*/

    entityManager.persist(c);
    entityManager.flush();
    entityTransaction.commit();
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void saveProperty(FStoreProperty p) {
    logger.info("Will FStoreProperty = " + p.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*  w  w w.  j  a  va2 s. c  o m*/

    entityManager.persist(p);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void saveProduct(Product prod) {
    logger.info("Will save Product = " + prod.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*  w w  w  .  j  a  va2 s . co m*/
    entityManager.persist(prod);
    entityManager.flush();
    entityTransaction.commit();

}