Search in sources :

Example 1 with QueryStringInteraction

use of org.eclipse.persistence.eis.interactions.QueryStringInteraction in project eclipselink by eclipse-ee4j.

the class MongoDatabaseTestSuite method testNativeQuery.

/**
 * Test native query.
 */
public void testNativeQuery() {
    EntityManager em = createEntityManager();
    MappedInteraction interaction = new MappedInteraction();
    interaction.setProperty(MongoPlatform.OPERATION, MongoOperation.FIND.name());
    interaction.setProperty(MongoPlatform.COLLECTION, "ORDER");
    interaction.setProperty(MongoPlatform.BATCH_SIZE, "10");
    interaction.setProperty(MongoPlatform.READ_PREFERENCE, "PRIMARY");
    interaction.addArgumentValue("_id", existingOrder.id);
    Query query = em.unwrap(JpaEntityManager.class).createQuery(interaction);
    List result = query.getResultList();
    if ((result.size() != 1) || (!(result.get(0) instanceof DataRecord)) || !(((DataRecord) result.get(0)).get("_id").equals(existingOrder.id))) {
        fail("Incorrect result: " + result);
    }
    interaction = new MappedInteraction();
    interaction.setProperty(MongoPlatform.OPERATION, MongoOperation.FIND.name());
    interaction.setProperty(MongoPlatform.COLLECTION, "ORDER");
    interaction.setProperty(MongoPlatform.BATCH_SIZE, "10");
    interaction.setProperty(MongoPlatform.READ_PREFERENCE, "PRIMARY");
    interaction.addArgumentValue("_id", existingOrder.id);
    query = em.unwrap(JpaEntityManager.class).createQuery(interaction, Order.class);
    Order order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
    QueryStringInteraction mqlInteraction = new QueryStringInteraction();
    mqlInteraction.setQueryString("db.ORDER.findOne({\"_id\":\"" + existingOrder.id + "\"})");
    query = em.unwrap(JpaEntityManager.class).createQuery(mqlInteraction, Order.class);
    order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
    query = em.createNativeQuery("db.ORDER.findOne({\"_id\":\"" + existingOrder.id + "\"})", Order.class);
    order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.mongo.Order) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) QueryStringInteraction(org.eclipse.persistence.eis.interactions.QueryStringInteraction) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ArrayList(java.util.ArrayList) List(java.util.List) DataRecord(org.eclipse.persistence.sessions.DataRecord)

Example 2 with QueryStringInteraction

use of org.eclipse.persistence.eis.interactions.QueryStringInteraction in project eclipselink by eclipse-ee4j.

the class MongoTestSuite method testNativeQuery.

/**
 * Test native query.
 */
public void testNativeQuery() {
    EntityManager em = createEntityManager();
    MappedInteraction interaction = new MappedInteraction();
    interaction.setProperty(MongoPlatform.OPERATION, MongoOperation.FIND.name());
    interaction.setProperty(MongoPlatform.COLLECTION, "ORDER");
    interaction.setProperty(MongoPlatform.BATCH_SIZE, "10");
    interaction.setProperty(MongoPlatform.READ_PREFERENCE, "PRIMARY");
    interaction.addArgumentValue("_id", existingOrder.id);
    Query query = em.unwrap(JpaEntityManager.class).createQuery(interaction);
    List result = query.getResultList();
    if ((result.size() != 1) || (!(result.get(0) instanceof DataRecord)) || !(((DataRecord) result.get(0)).get("_id").equals(existingOrder.id))) {
        fail("Incorrect result: " + result);
    }
    interaction = new MappedInteraction();
    interaction.setProperty(MongoPlatform.OPERATION, MongoOperation.FIND.name());
    interaction.setProperty(MongoPlatform.COLLECTION, "ORDER");
    interaction.setProperty(MongoPlatform.BATCH_SIZE, "10");
    interaction.setProperty(MongoPlatform.READ_PREFERENCE, "PRIMARY");
    interaction.addArgumentValue("_id", existingOrder.id);
    query = em.unwrap(JpaEntityManager.class).createQuery(interaction, Order.class);
    Order order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
    QueryStringInteraction mqlInteraction = new QueryStringInteraction();
    mqlInteraction.setQueryString("db.ORDER.findOne({\"_id\":\"" + existingOrder.id + "\"})");
    query = em.unwrap(JpaEntityManager.class).createQuery(mqlInteraction, Order.class);
    order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
    query = em.createNativeQuery("db.ORDER.findOne({\"_id\":\"" + existingOrder.id + "\"})", Order.class);
    order = (Order) query.getSingleResult();
    if ((order == null) || (!order.id.equals(existingOrder.id))) {
        fail("Incorrect result: " + order);
    }
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.mongo.Order) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) QueryStringInteraction(org.eclipse.persistence.eis.interactions.QueryStringInteraction) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ArrayList(java.util.ArrayList) List(java.util.List) DataRecord(org.eclipse.persistence.sessions.DataRecord)

Aggregations

EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MappedInteraction (org.eclipse.persistence.eis.interactions.MappedInteraction)2 QueryStringInteraction (org.eclipse.persistence.eis.interactions.QueryStringInteraction)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 DataRecord (org.eclipse.persistence.sessions.DataRecord)2 Order (org.eclipse.persistence.testing.models.jpa.mongo.Order)2