Search in sources :

Example 96 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class CustomSchema method storeLoadCheckSingleRowInvalid.

@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadCheckSingleRowInvalid() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        // Wrong start/end time
        Item item = new Item("Some Item", CalendarUtil.TOMORROW.getTime(), CalendarUtil.TODAY.getTime());
        em.persist(item);
        try {
            em.flush();
        } catch (Exception ex) {
            throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.complexschemas.custom.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 97 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class CustomSchema method storeLoadValid.

// The control
@Test
public void storeLoadValid() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        User user = new User();
        user.setEmail("valid@test.com");
        user.setUsername("someuser");
        em.persist(user);
        user = new User();
        user.setEmail("valid2@test.com");
        user.setUsername("otheruser");
        em.persist(user);
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        user = em.find(User.class, user.getId());
        assertEquals(user.getUsername(), "otheruser");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.complexschemas.custom.User) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 98 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class HelloWorldJPA method storeLoadMessage.

@Test
public void storeLoadMessage() throws Exception {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
    try {
        {
            /* 
                    Get access to the standard transaction API <code>UserTransaction</code> and
                    begin a transaction on this thread of execution.
                 */
            UserTransaction tx = TM.getUserTransaction();
            tx.begin();
            /* 
                    Begin a new session with the database by creating an <code>EntityManager</code>, this
                    is your context for all persistence operations.
                 */
            EntityManager em = emf.createEntityManager();
            /* 
                    Create a new instance of the mapped domain model class <code>Message</code> and
                    set its <code>text</code> property.
                 */
            Message message = new Message();
            message.setText("Hello World!");
            /* 
                    Enlist the transient instance with your persistence context, you make it persistent.
                    Hibernate now knows that you wish to store that data, it doesn't necessarily call the
                    database immediately, however.
                 */
            em.persist(message);
            /* 
                    Commit the transaction, Hibernate now automatically checks the persistence context and
                    executes the necessary SQL <code>INSERT</code> statement.
                 */
            tx.commit();
            // INSERT into MESSAGE (ID, TEXT) values (1, 'Hello World!')
            /* 
                    If you create an <code>EntityManager</code>, you must close it.
                 */
            em.close();
        }
        {
            /* 
                    Every interaction with your database should occur within explicit transaction boundaries,
                    even if you are only reading data.
                 */
            UserTransaction tx = TM.getUserTransaction();
            tx.begin();
            EntityManager em = emf.createEntityManager();
            /* 
                    Execute a query to retrieve all instances of <code>Message</code> from the database.
                 */
            List<Message> messages = em.createQuery("select m from Message m").getResultList();
            // SELECT * from MESSAGE
            assertEquals(messages.size(), 1);
            assertEquals(messages.get(0).getText(), "Hello World!");
            /* 
                    You can change the value of a property, Hibernate will detect this automatically because
                    the loaded <code>Message</code> is still attached to the persistence context it was loaded in.
                 */
            messages.get(0).setText("Take me to your leader!");
            /* 
                    On commit, Hibernate checks the persistence context for dirty state and executes the
                    SQL <code>UPDATE</code> automatically to synchronize the in-memory with the database state.
                 */
            tx.commit();
            // UPDATE MESSAGE set TEXT = 'Take me to your leader!' where ID = 1
            em.close();
        }
    } finally {
        TM.rollback();
        emf.close();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) Message(org.jpwh.model.helloworld.Message) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) Test(org.testng.annotations.Test) TransactionManagerTest(org.jpwh.env.TransactionManagerTest)

Example 99 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class AccessType method storeLoadAccessType.

@Test
public void storeLoadAccessType() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertEquals(item.getName(), "AUCTION: Some item");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 100 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class DerivedProperties method storeItemAndBids.

public Long storeItemAndBids() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    tx.begin();
    EntityManager em = JPA.createEntityManager();
    Item item = new Item();
    item.setName("Some item");
    item.setDescription("This is some description.");
    em.persist(item);
    for (int i = 1; i <= 3; i++) {
        Bid bid = new Bid();
        bid.setAmount(new BigDecimal(10 + i));
        bid.setItem(item);
        em.persist(bid);
    }
    tx.commit();
    em.close();
    return item.getId();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Bid(org.jpwh.model.advanced.Bid) BigDecimal(java.math.BigDecimal)

Aggregations

UserTransaction (javax.transaction.UserTransaction)642 EntityManager (javax.persistence.EntityManager)244 Test (org.testng.annotations.Test)187 Test (org.junit.Test)157 JPATest (org.jpwh.env.JPATest)138 InitialContext (javax.naming.InitialContext)62 BigDecimal (java.math.BigDecimal)53 HashMap (java.util.HashMap)52 IOException (java.io.IOException)50 Context (javax.naming.Context)49 List (java.util.List)47 FacesContext (javax.faces.context.FacesContext)43 NamingException (javax.naming.NamingException)43 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 Node (javax.jcr.Node)42 KieSession (org.kie.api.runtime.KieSession)42 Query (javax.persistence.Query)38 QueryingTest (org.jpwh.test.querying.QueryingTest)36 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 Item (org.jpwh.model.querying.Item)35