Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

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

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

Usage

From source file:org.croodie.resource.UserServerResource.java

@Get
public Representation retrieveUsers(Representation entity) {
    Form form = new Form(entity);
    String id = form.getFirstValue("id");
    logger.info("Fetching user with id: " + id);
    EntityManagerFactory emf = EMF.get();
    EntityManager em = emf.createEntityManager();
    User user = null;//  w w w.  j a v a2 s  .c om
    try {
        user = em.find(User.class, id);
    } finally {
        em.close();
    }
    return null;
}

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

@Test
public void testAddAnswerText() throws Exception {
    SurveyValue sv = getSurveyValue("addAnswerText.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    Question q = survey.getQuestions().get(0);
    assertNotNull(q);/*w  ww. j  ava 2  s  .c  o m*/
    assertEquals(5, q.getAnswers().size());
}

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

@Test
public void testLessAnswerText() throws Exception {
    SurveyValue sv = getSurveyValue("lessAnswerText.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    Question q = survey.getQuestions().get(0);
    assertNotNull(q);/*  w  ww  .j a  v a 2s .c  o  m*/
    assertEquals(3, q.getAnswers().size());
}

From source file:bq.jpa.demo.basic2.dao.AccountDaoImpl.java

/**
 * inject entitymanagerfactory, can support bean-transaction
 *///from ww  w.j av  a2s.  c  o  m
@Override
public Account addWithBeanTransaction(Account account) {
    EntityManager localEM = emf.createEntityManager();
    localEM.getTransaction().begin();
    localEM.persist(account);
    localEM.getTransaction().commit();
    localEM.close();
    return account;
}

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

@Test
public void testSwitchQuestion() throws Exception {
    SurveyValue sv = getSurveyValue("switchQuestion.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    assertEquals(2, survey.getQuestions().size());
    Question q1 = survey.getQuestions().get(0);
    assertEquals("Multiple Frage", q1.getText());
    Question q2 = survey.getQuestions().get(1);
    assertEquals("Choose Frage", q2.getText());
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void createTroop(Troop troop) throws IllegalArgumentException {

    if (troop == null || troop.getId() != null || troop.getName() == null || troop.getMission() == null
            || troop.getAmountOfMoney() == null) {
        throw new IllegalArgumentException("Create troop called with wrong param");
    }/* ww w  .j  a  v a 2  s  . com*/
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(troop);
    em.getTransaction().commit();
    em.close();
}

From source file:com.soen.ebanking.dao.ObjectDao.java

public T getObjectById(long id, Class<T> ClassName) {
    EntityManager em = this.getEMF().createEntityManager();
    try {//from   ww w.  j  a  va  2  s . c o m
        return em.find(ClassName, id);
    } finally {
        em.close();
    }
}

From source file:org.croodie.resource.UserServerResource.java

@Delete
public Representation deleteUser(Representation entity) {
    Form form = new Form(entity);
    String id = form.getFirstValue("id");
    EntityManagerFactory emf = EMF.get();
    EntityManager em = emf.createEntityManager();
    try {//from w w  w . ja  v a  2  s.  c o m
        User user = em.find(User.class, id);
        em.remove(user);
    } finally {
        em.close();
    }
    return null;
}

From source file:com.srotya.tau.api.dao.TestRulesManager.java

@BeforeClass
public static void beforeClass() throws Exception {
    java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);
    Properties config = new Properties(System.getProperties());
    File db = new File(TARGET_RULES_DB);
    if (db.exists()) {
        FileUtils.deleteDirectory(db);//  www . j a  v a2 s . c o  m
    }
    config.setProperty("javax.persistence.jdbc.url", CONNECTION_STRING);
    try {
        emf = Persistence.createEntityManagerFactory("tau", config);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

    EntityManager em = emf.createEntityManager();

    RuleGroup ruleGroup = new RuleGroup();
    ruleGroup.setRuleGroupId(RULE_GROUP_ID_1);
    ruleGroup.setRuleGroupName(TEST_RULE_GROUP);
    RuleGroupManager.getInstance().createRuleGroup(em, ruleGroup);

    ruleGroup = new RuleGroup();
    ruleGroup.setRuleGroupId(RULE_GROUP_ID_2);
    ruleGroup.setRuleGroupName(TEST_RULE_GROUP);
    RuleGroupManager.getInstance().createRuleGroup(em, ruleGroup);

    ruleGroup = new RuleGroup();
    ruleGroup.setRuleGroupId(RULE_GROUP_ID_3);
    ruleGroup.setRuleGroupName(TEST_RULE_GROUP);
    RuleGroupManager.getInstance().createRuleGroup(em, ruleGroup);

    ruleGroup = new RuleGroup();
    ruleGroup.setRuleGroupId(RULE_GROUP_ID_5);
    ruleGroup.setRuleGroupName(TEST_RULE_GROUP);
    RuleGroupManager.getInstance().createRuleGroup(em, ruleGroup);

    em.close();
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public Troop getTroop(Long id) throws IllegalArgumentException {
    if (id == null) {
        throw new IllegalArgumentException("getTroop called with null");
    }//from  ww w.jav  a2s  .c  om
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Troop troop = em.find(Troop.class, id);
    em.getTransaction().commit();
    em.close();
    return troop;
}