Search in sources :

Example 1 with Address

use of org.jpwh.model.advanced.Address in project microservices by pwillhan.

the class NestedComponents method storeAndLoadUsers.

@Test
public void storeAndLoadUsers() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        City city = new City();
        city.setZipcode("12345");
        city.setName("Some City");
        city.setCountry(Locale.GERMANY.getCountry());
        Address address = new Address();
        address.setStreet("Some Street 123");
        address.setCity(city);
        User userOne = new User();
        userOne.setAddress(address);
        em.persist(userOne);
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        User u = em.find(User.class, userOne.getId());
        assertEquals(u.getAddress().getStreet(), "Some Street 123");
        assertEquals(u.getAddress().getCity().getZipcode(), "12345");
        assertEquals(u.getAddress().getCity().getCountry(), Locale.GERMANY.getCountry());
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.advanced.User) Address(org.jpwh.model.advanced.Address) City(org.jpwh.model.advanced.City) 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 Address (org.jpwh.model.advanced.Address)1 City (org.jpwh.model.advanced.City)1 User (org.jpwh.model.advanced.User)1 Test (org.testng.annotations.Test)1