Search in sources :

Example 1 with Blue

use of org.eclipse.persistence.testing.models.jpa.inherited.Blue 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 Blue

use of org.eclipse.persistence.testing.models.jpa.inherited.Blue 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 blueLightJoin = qb.treat(root.join("blueBeersToConsume"), BlueLight.class);
        cq.where(qb.equal(blueLightJoin.get("discount"), 10));
        List 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) Join(jakarta.persistence.criteria.Join) BlueLight(org.eclipse.persistence.testing.models.jpa.inherited.BlueLight) List(java.util.List) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)

Example 3 with Blue

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

the class JUnitCriteriaSimpleTestSuiteBase method mapCastTest.

public void mapCastTest() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer bc1 = new BeerConsumer();
        bc1.setName("George");
        em.persist(bc1);
        Blue blue = new Blue();
        blue.setUniqueKey(new BigInteger("1"));
        em.persist(blue);
        bc1.addBlueBeerToConsume(blue);
        blue.setBeerConsumer(bc1);
        BeerConsumer bc2 = new BeerConsumer();
        bc2.setName("Scott");
        em.persist(bc2);
        BlueLight blueLight = new BlueLight();
        blueLight.setDiscount(10);
        blueLight.setUniqueKey(new BigInteger("2"));
        em.persist(blueLight);
        blueLight.setBeerConsumer(bc2);
        bc2.addBlueBeerToConsume(blueLight);
        em.flush();
        em.clear();
        clearCache();
        ReadAllQuery query = new ReadAllQuery();
        Expression selectionCriteria = new ExpressionBuilder().anyOf("blueBeersToConsume").treat(BlueLight.class).get("discount").equal(10);
        query.setSelectionCriteria(selectionCriteria);
        query.setReferenceClass(BeerConsumer.class);
        query.dontUseDistinct();
        Query jpaQuery = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em.getDelegate()).createQuery(query);
        List expectedResult = jpaQuery.getResultList();
        clearCache();
        em.clear();
        // "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
        CriteriaBuilder qb1 = em.getCriteriaBuilder();
        CriteriaQuery<BeerConsumer> cq1 = qb1.createQuery(BeerConsumer.class);
        Root<BeerConsumer> root = wrapper.from(cq1, BeerConsumer.class);
        Join<BeerConsumer, Blue> join = wrapper.join(root, BeerConsumer_blueBeersToConsume);
        jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(BlueLight.class), BlueLight_discount);
        cq1.where(qb1.equal(exp, 10));
        List<BeerConsumer> result = em.createQuery(cq1).getResultList();
        assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
    } finally {
        if (isTransactionActive(em)) {
            this.rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) TypedQuery(jakarta.persistence.TypedQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Blue(org.eclipse.persistence.testing.models.jpa.inherited.Blue) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) BlueLight(org.eclipse.persistence.testing.models.jpa.inherited.BlueLight) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Expression(org.eclipse.persistence.expressions.Expression) BigInteger(java.math.BigInteger) List(java.util.List) ArrayList(java.util.ArrayList) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)

Example 4 with Blue

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

the class JUnitJPQLComplexTestSuite method mapContainerPolicyMapValueInSelectionCriteriaTest.

public void mapContainerPolicyMapValueInSelectionCriteriaTest() {
    // skip test on OC4j some this test fails on some OC4j versions because of an issue with Timestamp
    if (getServerSession().getServerPlatform() != null && getServerSession().getServerPlatform() instanceof Oc4jPlatform) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer consumer = new BeerConsumer();
        consumer.setName("Marvin Monroe");
        em.persist(consumer);
        Blue blue = new Blue();
        blue.setAlcoholContent(5.0f);
        blue.setUniqueKey(BigInteger.ONE);
        consumer.addBlueBeerToConsume(blue);
        em.persist(blue);
        BeerConsumer consumer2 = new BeerConsumer();
        consumer2.setName("Marvin Monroe2");
        em.persist(consumer2);
        Blue blue2 = new Blue();
        blue2.setAlcoholContent(5.0f);
        blue2.setUniqueKey(BigInteger.valueOf(2));
        consumer2.addBlueBeerToConsume(blue2);
        em.persist(blue2);
        em.flush();
        Vector expectedResult = new Vector();
        expectedResult.add(consumer);
        clearCache();
        String ejbqlString = "SELECT bc FROM BeerConsumer bc join bc.blueBeersToConsume b where VALUE(b).uniqueKey = 1";
        List result = em.createQuery(ejbqlString).getResultList();
        Assert.assertTrue("mapContainerPolicyMapValueInSelectionCriteriaTest failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Blue(org.eclipse.persistence.testing.models.jpa.inherited.Blue) Oc4jPlatform(org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform) List(java.util.List) ArrayList(java.util.ArrayList) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) Vector(java.util.Vector)

Example 5 with Blue

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

the class JUnitJPQLComplexTestSuite method mapContainerPolicyMapKeyInSelectTest.

public void mapContainerPolicyMapKeyInSelectTest() {
    // skip test on OC4j some this test fails on some OC4j versions because of an issue with Timestamp
    if (getServerSession().getServerPlatform() != null && getServerSession().getServerPlatform() instanceof Oc4jPlatform) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer consumer = new BeerConsumer();
        consumer.setName("Marvin Monroe");
        em.persist(consumer);
        Blue blue = new Blue();
        blue.setAlcoholContent(5.0f);
        blue.setUniqueKey(BigInteger.ONE);
        consumer.addBlueBeerToConsume(blue);
        em.persist(blue);
        BeerConsumer consumer2 = new BeerConsumer();
        consumer2.setName("Marvin Monroe2");
        em.persist(consumer2);
        Blue blue2 = new Blue();
        blue2.setAlcoholContent(5.0f);
        blue2.setUniqueKey(BigInteger.valueOf(2));
        consumer2.addBlueBeerToConsume(blue2);
        em.persist(blue2);
        em.flush();
        Vector expectedResult = new Vector();
        expectedResult.add(BigInteger.ONE);
        clearCache();
        String ejbqlString = "SELECT KEY(b) FROM BeerConsumer bc join bc.blueBeersToConsume b where bc.name = 'Marvin Monroe'";
        List result = em.createQuery(ejbqlString).getResultList();
        Assert.assertTrue("mapContainerPolicyMapKeyInSelectTest failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Blue(org.eclipse.persistence.testing.models.jpa.inherited.Blue) Oc4jPlatform(org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform) List(java.util.List) ArrayList(java.util.ArrayList) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) Vector(java.util.Vector)

Aggregations

Blue (org.eclipse.persistence.testing.models.jpa.inherited.Blue)13 EntityManager (jakarta.persistence.EntityManager)11 BeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)11 List (java.util.List)8 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)8 ExpertBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer)7 ArrayList (java.util.ArrayList)6 Oc4jPlatform (org.eclipse.persistence.platform.server.oc4j.Oc4jPlatform)5 BigInteger (java.math.BigInteger)4 Vector (java.util.Vector)4 BlueLight (org.eclipse.persistence.testing.models.jpa.inherited.BlueLight)4 Query (jakarta.persistence.Query)3 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)3 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)3 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)3 ReportQuery (org.eclipse.persistence.queries.ReportQuery)3 TypedQuery (jakarta.persistence.TypedQuery)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Join (jakarta.persistence.criteria.Join)1 Map (java.util.Map)1