Search in sources :

Example 1 with CreditCard

use of org.jpwh.model.bulkbatch.CreditCard in project microservices by pwillhan.

the class BulkStatementsCriteria method bulkDelete.

@Test(expectedExceptions = EntityNotFoundException.class)
public void bulkDelete() throws Exception {
    BulkBatchTestData testData = storeTestData();
    UserTransaction tx = TM.getUserTransaction();
    try {
        CriteriaBuilder criteriaBuilder = JPA.getEntityManagerFactory().getCriteriaBuilder();
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        List<CreditCard> creditCards = em.createQuery("select c from CreditCard c").getResultList();
        CriteriaDelete<CreditCard> delete = criteriaBuilder.createCriteriaDelete(CreditCard.class);
        Root<CreditCard> c = delete.from(CreditCard.class);
        delete.where(criteriaBuilder.like(c.get(CreditCard_.owner), "J%"));
        em.createQuery(delete).executeUpdate();
        for (CreditCard creditCard : creditCards) {
            // Throws EntityNotFoundException
            em.refresh(creditCard);
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CreditCard(org.jpwh.model.bulkbatch.CreditCard) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with CreditCard

use of org.jpwh.model.bulkbatch.CreditCard in project microservices by pwillhan.

the class BulkStatementsCriteria method storeTestData.

public BulkBatchTestData storeTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] itemIds = new Long[3];
    Long[] userIds = new Long[3];
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    userIds[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    userIds[1] = janeroe.getId();
    User robertdoe = new User("robertdoe");
    em.persist(robertdoe);
    userIds[2] = robertdoe.getId();
    Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[1] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, janeroe, new BigDecimal(2 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item_Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
    em.persist(item);
    itemIds[2] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, johndoe, new BigDecimal(3 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    em.persist(new BankAccount(janeroe, "Jane Roe", "445566", "One Percent Bank Inc.", "999"));
    em.persist(new CreditCard(johndoe, "John Doe", "1234123412341234", "06", "2015"));
    tx.commit();
    em.close();
    BulkBatchTestData testData = new BulkBatchTestData();
    testData.items = new TestData(itemIds);
    testData.users = new TestData(userIds);
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.bulkbatch.User) TestData(org.jpwh.shared.util.TestData) BankAccount(org.jpwh.model.bulkbatch.BankAccount) BigDecimal(java.math.BigDecimal) CreditCard(org.jpwh.model.bulkbatch.CreditCard) Item(org.jpwh.model.bulkbatch.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.bulkbatch.Bid)

Example 3 with CreditCard

use of org.jpwh.model.bulkbatch.CreditCard in project microservices by pwillhan.

the class BulkStatementsJPQL method bulkDelete.

@Test(expectedExceptions = EntityNotFoundException.class)
public void bulkDelete() throws Exception {
    BulkBatchTestData testData = storeTestData();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        List<CreditCard> creditCards = em.createQuery("select c from CreditCard c").getResultList();
        em.createQuery("delete CreditCard c where c.owner like 'J%'").executeUpdate();
        for (CreditCard creditCard : creditCards) {
            // Throws EntityNotFoundException
            em.refresh(creditCard);
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) CreditCard(org.jpwh.model.bulkbatch.CreditCard) StolenCreditCard(org.jpwh.model.bulkbatch.StolenCreditCard) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 4 with CreditCard

use of org.jpwh.model.bulkbatch.CreditCard in project microservices by pwillhan.

the class BulkStatementsSQL method storeTestData.

public BulkBatchTestData storeTestData() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Long[] itemIds = new Long[3];
    Long[] userIds = new Long[3];
    User johndoe = new User("johndoe");
    em.persist(johndoe);
    userIds[0] = johndoe.getId();
    User janeroe = new User("janeroe");
    em.persist(janeroe);
    userIds[1] = janeroe.getId();
    User robertdoe = new User("robertdoe");
    em.persist(robertdoe);
    userIds[2] = robertdoe.getId();
    Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[0] = item.getId();
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
    em.persist(item);
    itemIds[1] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, janeroe, new BigDecimal(2 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    item = new Item("Item_Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
    em.persist(item);
    itemIds[2] = item.getId();
    for (int i = 1; i <= 1; i++) {
        Bid bid = new Bid(item, johndoe, new BigDecimal(3 + i));
        item.getBids().add(bid);
        em.persist(bid);
    }
    em.persist(new BankAccount(janeroe, "Jane Roe", "445566", "One Percent Bank Inc.", "999"));
    em.persist(new CreditCard(johndoe, "John Doe", "1234123412341234", "06", "2015"));
    tx.commit();
    em.close();
    BulkBatchTestData testData = new BulkBatchTestData();
    testData.items = new TestData(itemIds);
    testData.users = new TestData(userIds);
    return testData;
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.bulkbatch.User) TestData(org.jpwh.shared.util.TestData) BankAccount(org.jpwh.model.bulkbatch.BankAccount) BigDecimal(java.math.BigDecimal) CreditCard(org.jpwh.model.bulkbatch.CreditCard) StolenCreditCard(org.jpwh.model.bulkbatch.StolenCreditCard) Item(org.jpwh.model.bulkbatch.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.bulkbatch.Bid)

Example 5 with CreditCard

use of org.jpwh.model.bulkbatch.CreditCard in project microservices by pwillhan.

the class BulkStatementsJPQL method bulkUpdateInheritance.

@Test
public void bulkUpdateInheritance() throws Exception {
    BulkBatchTestData testData = storeTestData();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        // Hibernate even creates a temporary table for this update
        Query query = em.createQuery("update CreditCard c set c.stolenOn = :now where c.owner like 'J%'").setParameter("now", new Date());
        int updatedEntities = query.executeUpdate();
        assertEquals(updatedEntities, 1);
        List<CreditCard> creditCards = em.createQuery("select c from CreditCard c").getResultList();
        assertEquals(creditCards.size(), 1);
        for (CreditCard creditCard : creditCards) {
            assertTrue(creditCard.getStolenOn().getTime() > 0);
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Date(java.util.Date) CreditCard(org.jpwh.model.bulkbatch.CreditCard) StolenCreditCard(org.jpwh.model.bulkbatch.StolenCreditCard) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)6 UserTransaction (javax.transaction.UserTransaction)6 CreditCard (org.jpwh.model.bulkbatch.CreditCard)6 StolenCreditCard (org.jpwh.model.bulkbatch.StolenCreditCard)4 BigDecimal (java.math.BigDecimal)3 JPATest (org.jpwh.env.JPATest)3 BankAccount (org.jpwh.model.bulkbatch.BankAccount)3 Bid (org.jpwh.model.bulkbatch.Bid)3 Item (org.jpwh.model.bulkbatch.Item)3 User (org.jpwh.model.bulkbatch.User)3 TestData (org.jpwh.shared.util.TestData)3 Test (org.testng.annotations.Test)3 Date (java.util.Date)1 Query (javax.persistence.Query)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1