use of org.jpwh.model.associations.onetoone.foreignkey.Address in project microservices by pwillhan.
the class OneToOneForeignKey method storeNonUniqueRelationship.
@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeNonUniqueRelationship() throws Throwable {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Address someAddress = new Address("Some Street 123", "12345", "Some City");
User userOne = new User("johndoe");
userOne.setShippingAddress(someAddress);
// OK
em.persist(userOne);
User userTwo = new User("janeroe");
userTwo.setShippingAddress(someAddress);
// Fails, true unique @OneToOne!
em.persist(userTwo);
try {
// Hibernate tries the INSERT but fails
em.flush();
} catch (Exception ex) {
throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
}
} finally {
TM.rollback();
}
}
use of org.jpwh.model.associations.onetoone.foreignkey.Address in project microservices by pwillhan.
the class OneToOneForeignKey method storeAndLoadUserAddress.
@Test
public void storeAndLoadUserAddress() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
User someUser = new User("johndoe");
Address someAddress = new Address("Some Street 123", "12345", "Some City");
// Link
someUser.setShippingAddress(someAddress);
// Transitive persistence of shippingAddress
em.persist(someUser);
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");
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
Aggregations