Search in sources :

Example 1 with Address

use of org.jpwh.model.associations.onetoone.sharedprimarykey.Address in project microservices by pwillhan.

the class OneToOneSharedPrimaryKey method storeAndLoadUserAddress.

@Test
public void storeAndLoadUserAddress() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Address someAddress = new Address("Some Street 123", "12345", "Some City");
        // Generate identifier value
        em.persist(someAddress);
        User someUser = new User(// Assign same identifier value
        someAddress.getId(), "johndoe");
        em.persist(someUser);
        // Optional...
        someUser.setShippingAddress(someAddress);
        tx.commit();
        em.close();
        Long USER_ID = someUser.getId();
        Long ADDRESS_ID = someAddress.getId();
        tx.begin();
        em = JPA.createEntityManager();
        User user = em.find(User.class, USER_ID);
        assertEquals(user.getShippingAddress().getZipcode(), "12345");
        Address address = em.find(Address.class, ADDRESS_ID);
        assertEquals(address.getZipcode(), "12345");
        assertEquals(user.getId(), address.getId());
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.associations.onetoone.sharedprimarykey.User) Address(org.jpwh.model.associations.onetoone.sharedprimarykey.Address) 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.associations.onetoone.sharedprimarykey.Address)1 User (org.jpwh.model.associations.onetoone.sharedprimarykey.User)1 Test (org.testng.annotations.Test)1