Search in sources :

Example 1 with BeerConsumer

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

the class CriteriaQueryCastTestSuite method testDowncastSingleTableQueryKey.

public void testDowncastSingleTableQueryKey() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer consumer = new BeerConsumer();
        consumer.setName("John");
        em.persist(consumer);
        Blue blue = new Blue();
        blue.setAlcoholContent(5f);
        blue.setUniqueKey(new BigInteger("4531"));
        em.persist(blue);
        BlueLight blueLight = new BlueLight();
        blueLight.setDiscount(10);
        blueLight.setUniqueKey(new BigInteger("4533"));
        em.persist(blueLight);
        consumer.addBlueBeerToConsume(blueLight);
        blueLight.setBeerConsumer(consumer);
        consumer.addBlueBeerToConsume(blue);
        consumer.addBlueBeerToConsume(blueLight);
        consumer = new BeerConsumer();
        consumer.setName("Frank");
        em.persist(consumer);
        blueLight = new BlueLight();
        blueLight.setDiscount(5);
        blueLight.setUniqueKey(new BigInteger("4532"));
        em.persist(blueLight);
        consumer.addBlueBeerToConsume(blueLight);
        blueLight.setBeerConsumer(consumer);
        em.flush();
        clearCache();
        em.clear();
        // Query query = em.createQuery("Select b from BeerConsumer b join treat(b.blueBeersToConsume as BlueLight) bl where bl.discount = 10");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<BeerConsumer> cq = qb.createQuery(BeerConsumer.class);
        Root<BeerConsumer> root = cq.from(BeerConsumer.class);
        Join<Object, BlueLight> blueLightJoin = qb.treat(root.join("blueBeersToConsume"), BlueLight.class);
        cq.where(qb.equal(blueLightJoin.get("discount"), 10));
        List<BeerConsumer> resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Blue(org.eclipse.persistence.testing.models.jpa.inherited.Blue) BigInteger(java.math.BigInteger) BlueLight(org.eclipse.persistence.testing.models.jpa.inherited.BlueLight) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)

Example 2 with BeerConsumer

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

the class InheritedModelJunitTest method testVersionUpdateOnElementCollectionChange.

public void testVersionUpdateOnElementCollectionChange() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer beerConsumer = (BeerConsumer) em.createQuery("select b from BeerConsumer b join b.redStripes r").getResultList().get(0);
        int currentVersion = beerConsumer.getVersion();
        beerConsumer.getRedStripes().put("version", new RedStripe(Double.valueOf("343")));
        commitTransaction(em);
        assertTrue("Did not increment version for change to element collection", beerConsumer.getVersion() == ++currentVersion);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) RedStripe(org.eclipse.persistence.testing.models.jpa.inherited.RedStripe) 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 3 with BeerConsumer

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

the class InheritedModelJunitTest method testBecksBeerConsumer.

public void testBecksBeerConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    BeerConsumer initialBC = new BeerConsumer();
    int beerConsumerId = 0;
    try {
        Becks becks1 = new Becks();
        becks1.setAlcoholContent(5.1);
        BecksTag becksTag1 = new BecksTag();
        becksTag1.setCallNumber("0A.789");
        em.persist(becksTag1);
        Becks becks2 = new Becks();
        becks2.setAlcoholContent(5.1);
        BecksTag becksTag2 = new BecksTag();
        becksTag2.setCallNumber("BX.521");
        em.persist(becksTag2);
        Becks becks3 = new Becks();
        becks3.setAlcoholContent(5.1);
        BecksTag becksTag3 = new BecksTag();
        becksTag3.setCallNumber("UY.429");
        em.persist(becksTag3);
        initialBC.setName("Becks Consumer");
        initialBC.addBecksBeerToConsume(becks1, becksTag1);
        initialBC.addBecksBeerToConsume(becks2, becksTag2);
        initialBC.addBecksBeerToConsume(becks3, becksTag3);
        em.persist(initialBC);
        beerConsumerId = initialBC.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    BeerConsumer refreshedBC = em.find(BeerConsumer.class, beerConsumerId);
    assertTrue("The beer consumer read back did not match the original", getServerSession().compareObjects(initialBC, refreshedBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) Becks(org.eclipse.persistence.testing.models.jpa.inherited.Becks) 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) BecksTag(org.eclipse.persistence.testing.models.jpa.inherited.BecksTag)

Example 4 with BeerConsumer

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

the class InheritedModelJunitTest method testEmbeddableAggregateCollectionAndAggregate.

// Bug 334175
public void testEmbeddableAggregateCollectionAndAggregate() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    BeerConsumer consumer = new BeerConsumer();
    consumer.setName("Lionel");
    RedStripe rs = new RedStripe();
    rs.setAlcoholContent(4.5);
    consumer.addRedStripeByAlcoholContent(rs);
    em.persist(consumer);
    Official official = new Official();
    official.setName("George");
    rs = new RedStripe();
    rs.setAlcoholContent(4.6);
    official.setLastRedStripeConsumed(rs);
    em.persist(official);
    em.flush();
    em.clear();
    clearCache();
    consumer = em.find(BeerConsumer.class, consumer.getId());
    assertNotNull("BeerConsumer had null red stripes.", consumer.getRedStripes());
    assertTrue("BeerConsumer had wrong number of red stripes.", consumer.getRedStripesByAlcoholContent().size() == 1);
    assertTrue("BeerConsumer had wrong red stripe.", consumer.getRedStripesByAlcoholContent().get(4.5) != null);
    official = em.find(Official.class, official.getId());
    assertNotNull("Official had null red stripe.", official.getLastRedStripeConsumed());
    assertTrue("Official had the wrong red stripe", official.getLastRedStripeConsumed().getAlcoholContent() == 4.6);
    rollbackTransaction(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) RedStripe(org.eclipse.persistence.testing.models.jpa.inherited.RedStripe) Official(org.eclipse.persistence.testing.models.jpa.inherited.Official) 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 5 with BeerConsumer

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

the class InheritedModelJunitTest method testMapOrphanRemoval.

public void testMapOrphanRemoval() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    BeerConsumer initialBC = new BeerConsumer();
    int beerConsumerId = 0;
    try {
        Becks becks1 = new Becks();
        becks1.setAlcoholContent(5.1);
        BecksTag becksTag1 = new BecksTag();
        becksTag1.setCallNumber("0A.789");
        em.persist(becksTag1);
        initialBC.setName("Becks Consumer");
        initialBC.addBecksBeerToConsume(becks1, becksTag1);
        em.persist(initialBC);
        beerConsumerId = initialBC.getId();
        em.flush();
        clearCache();
        BeerConsumer refreshedBC = em.find(BeerConsumer.class, beerConsumerId);
        refreshedBC.getBecksBeersToConsume().remove(becksTag1);
        em.flush();
        clearCache();
        becksTag1 = em.find(BecksTag.class, becksTag1.getId());
        assertTrue("Key was deleted when it should not be.", becksTag1 != null);
        becks1 = em.find(Becks.class, becks1.getId());
        assertTrue("Orphan removal did not remove the orphan", becks1 == null);
        rollbackTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation: [" + e.getMessage() + "]");
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Becks(org.eclipse.persistence.testing.models.jpa.inherited.Becks) 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) BecksTag(org.eclipse.persistence.testing.models.jpa.inherited.BecksTag)

Aggregations

BeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)60 EntityManager (jakarta.persistence.EntityManager)57 ExpertBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer)37 NoviceBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)18 List (java.util.List)15 Vector (java.util.Vector)15 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)15 ArrayList (java.util.ArrayList)13 Oc4jPlatform (org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform)12 Alpine (org.eclipse.persistence.testing.models.jpa.inherited.Alpine)11 Blue (org.eclipse.persistence.testing.models.jpa.inherited.Blue)11 SerialNumber (org.eclipse.persistence.testing.models.jpa.inherited.SerialNumber)9 Date (java.util.Date)7 BigInteger (java.math.BigInteger)6 Becks (org.eclipse.persistence.testing.models.jpa.inherited.Becks)6 BecksTag (org.eclipse.persistence.testing.models.jpa.inherited.BecksTag)6 BlueLight (org.eclipse.persistence.testing.models.jpa.inherited.BlueLight)6 RedStripe (org.eclipse.persistence.testing.models.jpa.inherited.RedStripe)4 TelephoneNumber (org.eclipse.persistence.testing.models.jpa.inherited.TelephoneNumber)4 Query (jakarta.persistence.Query)3