Search in sources :

Example 1 with UserId

use of org.jpwh.model.complexschemas.compositekey.manytoone.UserId in project microservices by pwillhan.

the class CompositeKeyManyToOne method storeLoad.

@Test
public void storeLoad() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        {
            UserId id = new UserId("johndoe", "123");
            User user = new User(id);
            em.persist(user);
            Item item = new Item("Some Item");
            item.setSeller(user);
            em.persist(item);
        }
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        {
            UserId id = new UserId("johndoe", "123");
            User user = em.find(User.class, id);
            assertEquals(user.getId().getDepartmentNr(), "123");
            Item item = (Item) em.createQuery("select i from Item i where i.seller = :u").setParameter("u", user).getSingleResult();
            assertEquals(item.getName(), "Some Item");
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.complexschemas.compositekey.manytoone.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.complexschemas.compositekey.manytoone.User) UserId(org.jpwh.model.complexschemas.compositekey.manytoone.UserId) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1 UserTransaction (javax.transaction.UserTransaction)1 JPATest (org.jpwh.env.JPATest)1 Item (org.jpwh.model.complexschemas.compositekey.manytoone.Item)1 User (org.jpwh.model.complexschemas.compositekey.manytoone.User)1 UserId (org.jpwh.model.complexschemas.compositekey.manytoone.UserId)1 Test (org.testng.annotations.Test)1