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();
}
}
Aggregations