Search in sources :

Example 1 with Accredidation

use of org.eclipse.persistence.testing.models.jpa.inherited.Accredidation in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testBreakOrder_CorrectionType_READ_WRITE.

public void testBreakOrder_CorrectionType_READ_WRITE() {
    // create BeerConsumer with committees
    EntityManager em = createEntityManager();
    beginTransaction(em);
    ExpertBeerConsumer beerConsumer = new ExpertBeerConsumer();
    beerConsumer.setName("Beer order");
    beerConsumer.setAccredidation(new Accredidation());
    Committee committee0 = new Committee();
    committee0.setDescription("Broken Order 0");
    beerConsumer.addCommittee(committee0);
    Committee committee1 = new Committee();
    committee1.setDescription("Broken Order 1");
    beerConsumer.addCommittee(committee1);
    Committee committee2 = new Committee();
    committee2.setDescription("Broken Order 2");
    beerConsumer.addCommittee(committee2);
    try {
        em.persist(beerConsumer);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("failed to create beerConsumer");
    }
    Integer id = beerConsumer.getId();
    try {
        // break the order of committees
        beginTransaction(em);
        try {
            em.createNativeQuery("UPDATE JPA_CONSUMER_COMMITTEE SET ORDER_COLUMN = null WHERE CONSUMER_ID = " + id + " AND ORDER_COLUMN = 0").executeUpdate();
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            fail("failed to break order of committees");
        } finally {
            closeEntityManager(em);
        }
        // remove beerConsumer from the cache
        clearCache();
        // read back the beer consumer, reading of the list of committees with the broken should fix the order "on the fly":
        // the default correction performed on the list substitutes null in the first element back to 0;
        // then alter order of committees - that would cause the list order to be updated in the db (and become valid).
        em = createEntityManager();
        beginTransaction(em);
        try {
            beerConsumer = em.find(ExpertBeerConsumer.class, id);
            // committees #1 and #2 switch their positions in the list.
            Committee committee = beerConsumer.getCommittees().remove(2);
            beerConsumer.getCommittees().add(1, committee);
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
            fail("failed to change committees order");
        }
        // now verify that the order in the db has been corrected
        List<Number> results = em.createNativeQuery("SELECT ORDER_COLUMN FROM JPA_CONSUMER_COMMITTEE WHERE CONSUMER_ID = " + id + " ORDER BY ORDER_COLUMN").getResultList();
        int expectedSize = beerConsumer.getCommittees().size();
        if (expectedSize != results.size()) {
            closeEntityManager(em);
            fail("read in " + results.size() + " committees; expected " + expectedSize);
        }
        for (int i = 0; i < expectedSize; i++) {
            if (results.get(i) == null || results.get(i).intValue() != i) {
                closeEntityManager(em);
                fail("read in list in wrong order: " + results + "; expected: 0, 1, 2");
            }
        }
        closeEntityManager(em);
    } finally {
        // clean up
        em = createEntityManager();
        beginTransaction(em);
        try {
            beerConsumer = em.find(ExpertBeerConsumer.class, id);
            em.remove(beerConsumer);
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
        } finally {
            closeEntityManager(em);
        }
    }
}
Also used : BigInteger(java.math.BigInteger) EntityManager(jakarta.persistence.EntityManager) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation) Committee(org.eclipse.persistence.testing.models.jpa.inherited.Committee)

Example 2 with Accredidation

use of org.eclipse.persistence.testing.models.jpa.inherited.Accredidation in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testRedStripeExpertConsumer.

public void testRedStripeExpertConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    ExpertBeerConsumer initialEBC = new ExpertBeerConsumer();
    initialEBC.setAccredidation(new Accredidation());
    int beerConsumerId = 0;
    try {
        RedStripe redStripe1 = new RedStripe();
        redStripe1.setAlcoholContent(5.0);
        initialEBC.addRedStripeBeersToConsume(redStripe1, "1");
        RedStripe redStripe2 = new RedStripe();
        redStripe2.setAlcoholContent(5.0);
        initialEBC.addRedStripeBeersToConsume(redStripe2, "2");
        initialEBC.setName("Expert Red Stripe Consumer");
        em.persist(initialEBC);
        beerConsumerId = initialEBC.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        e.printStackTrace();
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    ExpertBeerConsumer refreshedEBC = em.find(ExpertBeerConsumer.class, beerConsumerId);
    assertTrue("The expert beer consumer read back did not match the original", getServerSession().compareObjects(initialEBC, refreshedEBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) RedStripe(org.eclipse.persistence.testing.models.jpa.inherited.RedStripe) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)

Example 3 with Accredidation

use of org.eclipse.persistence.testing.models.jpa.inherited.Accredidation in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testCreateExpertBeerConsumer.

public void testCreateExpertBeerConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    ExpertBeerConsumer beerConsumer = new ExpertBeerConsumer();
    try {
        beerConsumer.setName("Expert Beer Consumer");
        beerConsumer.setIQ(110);
        beerConsumer.getAcclaims().add("A");
        beerConsumer.getAcclaims().add("B");
        beerConsumer.getAcclaims().add("C");
        // Commenting out this mapping until bug 272298 is resolved.
        // The compareObjects check below would fail in some configurations
        // as a result of this.
        // beerConsumer.getAudio().add(new byte[]{1});
        // beerConsumer.getAudio().add(new byte[]{2});
        // beerConsumer.getAudio().add(new byte[]{3});
        beerConsumer.getAwards().put("A", "A");
        beerConsumer.getAwards().put("B", "B");
        beerConsumer.getAwards().put("C", "C");
        beerConsumer.getDesignations().add("A");
        beerConsumer.getDesignations().add("B");
        m_quote1Stamp = Helper.timestampFromDate(Helper.dateFromYearMonthDate(2009, 1, 1));
        beerConsumer.getQuotes().put(m_quote1Stamp, QUOTE_ONE);
        m_quote2Stamp = Helper.timestampFromDate(Helper.dateFromYearMonthDate(2005, 7, 9));
        beerConsumer.getQuotes().put(m_quote2Stamp, QUOTE_TWO);
        Record record1 = new Record();
        record1.setDescription("Fastest beer ever consumed - 10 ms");
        record1.setDate(Helper.dateFromYearMonthDate(2009, 10, 10));
        record1.setLocation(new Location("Ottawa", "Canada"));
        Venue venue1 = new Venue();
        venue1.setAttendance(10000);
        venue1.setName("Scotiabank PLace");
        record1.setVenue(venue1);
        beerConsumer.getRecords().add(record1);
        Record record2 = new Record();
        record2.setDescription("Most beers consumed upside down - 12");
        record2.setDate(Helper.dateFromYearMonthDate(2007, 11, 11));
        record2.setLocation(new Location("Sydney", "Australia"));
        Venue venue2 = new Venue();
        venue2.setAttendance(2678);
        venue2.setName("Opera House");
        record2.setVenue(venue2);
        beerConsumer.getRecords().add(record2);
        Record record3 = new Record();
        record3.setDescription("Most beers consumed in a second - 5");
        record3.setDate(Helper.dateFromYearMonthDate(2005, 12, 12));
        record3.setLocation(new Location("Miami", "USA"));
        Venue venue3 = new Venue();
        venue3.setAttendance(63000);
        venue3.setName("Dolphin Stadium");
        record3.setVenue(venue3);
        beerConsumer.getRecords().add(record3);
        Accredidation accredidation = new Accredidation();
        accredidation.setDetails("Elite, absolutely elite!");
        Witness witness1 = new Witness();
        witness1.setName("Big Bobby");
        accredidation.addWitness(witness1);
        Witness witness2 = new Witness();
        witness2.setName("Little Bobby");
        accredidation.addWitness(witness2);
        Official official = new Official();
        official.setName("Authority Joe");
        accredidation.addOfficial(official);
        beerConsumer.setAccredidation(accredidation);
        Birthday birthday1 = new Birthday();
        birthday1.setDay(9);
        birthday1.setMonth(7);
        birthday1.setYear(2005);
        beerConsumer.addCelebration(birthday1, "Drank a 24 of Heineken");
        Birthday birthday2 = new Birthday();
        birthday2.setDay(10);
        birthday2.setMonth(7);
        birthday2.setYear(2006);
        beerConsumer.addCelebration(birthday2, "Drank a 24 of Becks");
        Committee committee1 = new Committee();
        committee1.setDescription("New beer committee");
        CommitteeDates committee1Dates = new CommitteeDates();
        committee1Dates.setStartDate("Jan 1, 2010");
        committee1.setCommitteeDates(committee1Dates);
        beerConsumer.addCommittee(committee1);
        Committee committee2 = new Committee();
        committee2.setDescription("Alcohol content regulation");
        CommitteeDates committee2Dates = new CommitteeDates();
        committee2Dates.setStartDate("Jan 1, 1970");
        committee2Dates.setEndDate("Jan 1, 2001");
        committee2.setCommitteeDates(committee2Dates);
        beerConsumer.addCommittee(committee2);
        em.persist(beerConsumer);
        m_expertBeerConsumerId = beerConsumer.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation for an expert beer consumer: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    BeerConsumer refreshedBC = em.find(BeerConsumer.class, m_expertBeerConsumerId);
    assertTrue("The expert beer consumer read back did not match the original", getServerSession().compareObjects(beerConsumer, refreshedBC));
}
Also used : ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) Venue(org.eclipse.persistence.testing.models.jpa.inherited.Venue) Birthday(org.eclipse.persistence.testing.models.jpa.inherited.Birthday) Committee(org.eclipse.persistence.testing.models.jpa.inherited.Committee) CommitteeDates(org.eclipse.persistence.testing.models.jpa.inherited.CommitteeDates) EntityManager(jakarta.persistence.EntityManager) Official(org.eclipse.persistence.testing.models.jpa.inherited.Official) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation) Witness(org.eclipse.persistence.testing.models.jpa.inherited.Witness) Record(org.eclipse.persistence.testing.models.jpa.inherited.Record) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) Location(org.eclipse.persistence.testing.models.jpa.inherited.Location)

Example 4 with Accredidation

use of org.eclipse.persistence.testing.models.jpa.inherited.Accredidation in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testBreakOrder_CorrectionType_EXCEPTION.

public void testBreakOrder_CorrectionType_EXCEPTION() {
    // create BeerConsumer with designations
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer beerConsumer = new NoviceBeerConsumer();
    beerConsumer.setAccredidation(new Accredidation());
    beerConsumer.setName("Broken order");
    beerConsumer.getDesignations().add("0");
    beerConsumer.getDesignations().add("1");
    beerConsumer.getDesignations().add("2");
    try {
        em.persist(beerConsumer);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("failed to create beerConsumer");
    }
    Integer id = beerConsumer.getId();
    try {
        // break the order of designations
        beginTransaction(em);
        try {
            em.createNativeQuery("UPDATE NOVICE_CONSUMER_DESIGNATIONS SET ORDER_COLUMN = null WHERE NOVICE_CONSUMER_ID = " + id + " AND ORDER_COLUMN = 0").executeUpdate();
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            fail("failed to break order of designations");
        } finally {
            closeEntityManager(em);
        }
        // remove beerConsumer from the cache
        clearCache();
        // read back the beer consumer, reading of the list of designations with the broken order should trigger exception
        em = createEntityManager();
        try {
            beerConsumer = em.find(NoviceBeerConsumer.class, id);
            // trigger indirection
            beerConsumer.getDesignations().size();
            fail("Exception was expected - but not thrown");
        } catch (RuntimeException e) {
            boolean isCorrectException = false;
            if (e instanceof QueryException) {
                QueryException queryException = (QueryException) e;
                if (((QueryException) e).getErrorCode() == QueryException.LIST_ORDER_FIELD_WRONG_VALUE) {
                    isCorrectException = true;
                }
            }
            if (!isCorrectException) {
                throw e;
            }
        } finally {
            closeEntityManager(em);
        }
    } finally {
        // clean up
        em = createEntityManager();
        beginTransaction(em);
        try {
            beerConsumer = em.find(NoviceBeerConsumer.class, id);
            em.remove(beerConsumer);
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
        } finally {
            closeEntityManager(em);
        }
    }
}
Also used : BigInteger(java.math.BigInteger) EntityManager(jakarta.persistence.EntityManager) QueryException(org.eclipse.persistence.exceptions.QueryException) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)

Example 5 with Accredidation

use of org.eclipse.persistence.testing.models.jpa.inherited.Accredidation in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testRedStripeNoviceConsumer.

public void testRedStripeNoviceConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer initialNBC = new NoviceBeerConsumer();
    initialNBC.setAccredidation(new Accredidation());
    int beerConsumerId = 0;
    try {
        RedStripe redStripe1 = new RedStripe();
        redStripe1.setAlcoholContent(5.0);
        initialNBC.addRedStripeBeersToConsume(redStripe1, "3");
        RedStripe redStripe2 = new RedStripe();
        redStripe2.setAlcoholContent(5.0);
        initialNBC.addRedStripeBeersToConsume(redStripe2, "4");
        initialNBC.setName("Novice Red Stripe Consumer");
        em.persist(initialNBC);
        beerConsumerId = initialNBC.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        e.printStackTrace();
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    NoviceBeerConsumer refreshedNBC = em.find(NoviceBeerConsumer.class, beerConsumerId);
    assertTrue("The novice beer consumer read back did not match the original", getServerSession().compareObjects(initialNBC, refreshedNBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) RedStripe(org.eclipse.persistence.testing.models.jpa.inherited.RedStripe) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)

Aggregations

EntityManager (jakarta.persistence.EntityManager)10 Accredidation (org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)10 ExpertBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer)8 NoviceBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)3 Committee (org.eclipse.persistence.testing.models.jpa.inherited.Committee)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)2 Birthday (org.eclipse.persistence.testing.models.jpa.inherited.Birthday)2 Location (org.eclipse.persistence.testing.models.jpa.inherited.Location)2 Record (org.eclipse.persistence.testing.models.jpa.inherited.Record)2 RedStripe (org.eclipse.persistence.testing.models.jpa.inherited.RedStripe)2 Venue (org.eclipse.persistence.testing.models.jpa.inherited.Venue)2 Witness (org.eclipse.persistence.testing.models.jpa.inherited.Witness)2 Vector (java.util.Vector)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 Oc4jPlatform (org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform)1 CommitteeDates (org.eclipse.persistence.testing.models.jpa.inherited.CommitteeDates)1