Search in sources :

Example 1 with SerialNumber

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

the class InheritedModelJunitTest method testSerializedElementCollectionMap.

public void testSerializedElementCollectionMap() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer initialBC = new BeerConsumer();
        em.persist(initialBC);
        SerialNumber serialNumber = new SerialNumber();
        em.persist(serialNumber);
        initialBC.addCommentLookup(serialNumber, "A test comment");
        em.flush();
        em.clear();
        initialBC = em.find(BeerConsumer.class, initialBC.getId());
        serialNumber = em.find(SerialNumber.class, serialNumber.getNumber());
        assertTrue("The serialized map was not properly retrieved after persist.", initialBC.getCommentLookup().size() == 1);
        assertTrue("The serialized map did not contain the proper entry.", initialBC.getCommentLookup().get(serialNumber) != null);
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) 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)

Example 2 with SerialNumber

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

the class EmbeddableSuperclassJunitTest method testInsertNewAlpine.

public void testInsertNewAlpine() {
    Alpine alpine = null;
    EntityManager em = createEntityManager();
    // Part 1 ... add an alpine beer to the collection.
    BeerConsumer beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    beginTransaction(em);
    try {
        beerConsumer = em.merge(beerConsumer);
        SerialNumber serialNumber = new SerialNumber();
        em.persist(serialNumber);
        alpine = new Alpine(serialNumber);
        em.persist(alpine);
        alpine.setBestBeforeDate(Helper.dateFromYearMonthDate(2005, 8, 18));
        alpine.setAlcoholContent(5.4);
        alpine.setClassification(Alpine.Classification.BITTER);
        beerConsumer.addAlpineBeerToConsume(alpine, 0);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    // Part 2 ... read from cache.
    beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    // Read the beerConsumer back from the cache, check the ordering.
    Vector alpinesFromCache = (Vector) beerConsumer.getAlpineBeersToConsume();
    assertTrue("The new alpine was not added at the correct index in the cache.", alpinesFromCache.indexOf(alpine) == 0);
    // Part 3 ... read from database.
    clearCache();
    em.clear();
    beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    // Read the beerConsumer back from the database, check the ordering.
    Vector alpinesFromDB = (Vector) beerConsumer.getAlpineBeersToConsume();
    assertTrue("The new alpine was not added at the correct index when retrieving from the database.", alpinesFromDB.indexOf(alpine) == 1);
}
Also used : EntityManager(jakarta.persistence.EntityManager) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) Alpine(org.eclipse.persistence.testing.models.jpa.inherited.Alpine) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) Vector(java.util.Vector)

Example 3 with SerialNumber

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

the class EmbeddableSuperclassJunitTest method testInsertNewAlpineAndModifyOrderOfExistingAlpines.

public void testInsertNewAlpineAndModifyOrderOfExistingAlpines() {
    Alpine alpine1 = null, alpine2 = null;
    EntityManager em = createEntityManager();
    // Part 1 ... add an alpine beer to the collection.
    BeerConsumer beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    beginTransaction(em);
    try {
        beerConsumer = em.merge(beerConsumer);
        SerialNumber serialNumber = new SerialNumber();
        em.persist(serialNumber);
        alpine1 = new Alpine(serialNumber);
        em.persist(alpine1);
        alpine1.setBestBeforeDate(Helper.dateFromYearMonthDate(2005, 8, 16));
        alpine1.setAlcoholContent(5.6);
        alpine1.setClassification(Alpine.Classification.STRONG);
        beerConsumer.addAlpineBeerToConsume(alpine1, 0);
        // Part 2 ... change the date, hence the index, of an alpine.
        alpine2 = beerConsumer.getAlpineBeerToConsume(4);
        alpine2.setBestBeforeDate(Helper.dateFromYearMonthDate(2005, 8, 20));
        beerConsumer.moveAlpineBeerToConsume(4, 3);
        // Part 3 ... remove 2 alpines ...
        beerConsumer.removeAlpineBeerToConsume(4);
        beerConsumer.removeAlpineBeerToConsume(2);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    // Part 3 ... read from cache.
    beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    // Read the beerConsumer back from the cache, check the ordering.
    Vector alpinesFromCache = (Vector) beerConsumer.getAlpineBeersToConsume();
    assertTrue("The new alpine was not added at the correct index in the cache.", alpinesFromCache.indexOf(alpine1) == 0);
    assertTrue("The alpine was not moved to the correct index in the cache.", alpinesFromCache.indexOf(alpine2) == 2);
    // Part 4 ... read from database.
    clearCache();
    em.clear();
    beerConsumer = em.find(BeerConsumer.class, m_beerConsumerId2);
    // Read the beerConsumer back from the database, check the ordering.
    Vector alpinesFromDB = (Vector) beerConsumer.getAlpineBeersToConsume();
    assertTrue("The new alpine was not added at the correct index when retrieving from the database.", alpinesFromDB.indexOf(alpine1) == 0);
    assertTrue("The alpine was not moved to the correct index when retrieving from the database.", alpinesFromDB.indexOf(alpine2) == 2);
}
Also used : EntityManager(jakarta.persistence.EntityManager) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) Alpine(org.eclipse.persistence.testing.models.jpa.inherited.Alpine) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) Vector(java.util.Vector)

Example 4 with SerialNumber

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

the class InheritedCallbacksJunitTest method testPrePersistAlpineAndSerialNumberOnBeerConsumerMerge.

public void testPrePersistAlpineAndSerialNumberOnBeerConsumerMerge() {
    EntityManager em = createEntityManager();
    try {
        int beerPrePersistCount = Beer.BEER_PRE_PERSIST_COUNT;
        int alpinePrePersistCount = Alpine.ALPINE_PRE_PERSIST_COUNT;
        int serialNumberPrePersistCount = SerialNumber.SERIAL_NUMBER_PRE_PERSIST_COUNT;
        beginTransaction(em);
        BeerConsumer beerConsumer = new BeerConsumer();
        beerConsumer.setName("A consumer to delete eventually");
        em.persist(beerConsumer);
        em.clear();
        SerialNumber serialNumber = new SerialNumber();
        Alpine alpine = new Alpine(serialNumber);
        alpine.setBestBeforeDate(new java.util.Date(2007, 8, 17));
        alpine.setAlcoholContent(5.0);
        alpine.setClassification(Alpine.Classification.NONE);
        beerConsumer.addAlpineBeerToConsume(alpine);
        BeerConsumer mergedBeerConsumer = em.merge(beerConsumer);
        mergedBeerConsumer.getAlpineBeerToConsume(0).setId(mergedBeerConsumer.getAlpineBeerToConsume(0).getSerialNumber().getNumber());
        verifyNotCalled(beerPrePersistCount, Beer.BEER_PRE_PERSIST_COUNT, "PrePersist");
        verifyCalled(alpinePrePersistCount, Alpine.ALPINE_PRE_PERSIST_COUNT, "PrePersist");
        verifyCalled(serialNumberPrePersistCount, SerialNumber.SERIAL_NUMBER_PRE_PERSIST_COUNT, "PrePersist");
        assertTrue("The merged alpine classification was not updated from the PrePersist lifecycle method", mergedBeerConsumer.getAlpineBeerToConsume(0).getSerialNumber().getIssueDate() != null);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) Alpine(org.eclipse.persistence.testing.models.jpa.inherited.Alpine) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)

Example 5 with SerialNumber

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

the class InheritedCallbacksJunitTest method testPreAndPostPersistAlpine.

public void testPreAndPostPersistAlpine() {
    EntityManager em = createEntityManager();
    try {
        int beerPrePersistCount = Beer.BEER_PRE_PERSIST_COUNT;
        int alpinePrePersistCount = Alpine.ALPINE_PRE_PERSIST_COUNT;
        int beveragePostPersistCount = Beverage.BEVERAGE_POST_PERSIST_COUNT;
        int beerPostPersistCount = Beer.BEER_POST_PERSIST_COUNT;
        int alpinePostPersistCount = Alpine.ALPINE_POST_PERSIST_COUNT;
        beginTransaction(em);
        SerialNumber serialNumber = new SerialNumber();
        em.persist(serialNumber);
        Alpine alpine = new Alpine(serialNumber);
        alpine.setBestBeforeDate(new java.util.Date(2007, 8, 17));
        alpine.setAlcoholContent(5.0);
        em.persist(alpine);
        commitTransaction(em);
        verifyNotCalled(beerPrePersistCount, Beer.BEER_PRE_PERSIST_COUNT, "celebrate");
        verifyCalled(alpinePrePersistCount, Alpine.ALPINE_PRE_PERSIST_COUNT, "celebrate");
        verifyNotCalled(beveragePostPersistCount, Beverage.BEVERAGE_POST_PERSIST_COUNT, "celebrateAgain");
        verifyCalled(beerPostPersistCount, Beer.BEER_POST_PERSIST_COUNT, "celebrateSomeMore");
        verifyCalled(alpinePostPersistCount, Alpine.ALPINE_POST_PERSIST_COUNT, "celebrateAgain");
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) SerialNumber(org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber) Alpine(org.eclipse.persistence.testing.models.jpa.inherited.Alpine)

Aggregations

EntityManager (jakarta.persistence.EntityManager)11 SerialNumber (org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber)11 Alpine (org.eclipse.persistence.testing.models.jpa.inherited.Alpine)10 BeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)9 Date (java.util.Date)5 Vector (java.util.Vector)3 ExpertBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer)3 NoviceBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 BlueLight (org.eclipse.persistence.testing.models.jpa.inherited.BlueLight)1