Search in sources :

Example 1 with Address

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.associations.onetoone.foreignkey.User) Address(org.jpwh.model.associations.onetoone.foreignkey.Address) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 2 with Address

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.associations.onetoone.foreignkey.User) Address(org.jpwh.model.associations.onetoone.foreignkey.Address) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 JPATest (org.jpwh.env.JPATest)2 Address (org.jpwh.model.associations.onetoone.foreignkey.Address)2 User (org.jpwh.model.associations.onetoone.foreignkey.User)2 Test (org.testng.annotations.Test)2