Search in sources :

Example 1 with MappedInteraction

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

the class NoSQLSimpleTest method testNative.

/**
 * Test native Oracle NoSQL queries.
 */
@Test
public void testNative() throws Exception {
    final MappedInteraction insertCall = new MappedInteraction();
    insertCall.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.PUT.name());
    insertCall.addArgumentValue("Order/1234", "foo");
    final DataModifyQuery insert = new DataModifyQuery(insertCall);
    session.executeQuery(insert);
    final MappedInteraction readCall = new MappedInteraction();
    readCall.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
    readCall.addArgumentValue("Order/1234", "");
    final DataReadQuery read = new DataReadQuery(readCall);
    @SuppressWarnings("unchecked") final List<DataRecord> result = (List<DataRecord>) session.executeQuery(read);
    final String value = new String((byte[]) result.get(0).get("Order/1234"));
    assertEquals("foo expected: " + value, "foo", value);
}
Also used : MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) List(java.util.List) DataRecord(org.eclipse.persistence.sessions.DataRecord) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) Test(org.junit.Test)

Example 2 with MappedInteraction

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

the class NoSQLJPATest method testNativeQuery.

/**
 * Test native query.
 */
@Test
public void testNativeQuery() {
    MappedInteraction interaction = new MappedInteraction();
    final Order existingOrder = getRandomOrder();
    interaction.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
    interaction.addArgumentValue("[ORDER," + existingOrder.id + "]", "");
    final Query query1 = em.unwrap(JpaEntityManager.class).createQuery(interaction);
    @SuppressWarnings("unchecked") final List<Object> result1 = query1.getResultList();
    assertEquals(String.format("Expected result of size 1, got %d", result1.size()), 1, result1.size());
    assertTrue(String.format("Result is not instance of Record but %s", result1.get(0).getClass().getSimpleName()), (result1.get(0) instanceof DataRecord));
    assertTrue(String.format("Incorrect result: %s", result1), (((DataRecord) result1.get(0)).containsKey("[ORDER," + existingOrder.id + "]")));
    interaction = new XMLInteraction();
    interaction.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
    interaction.addArgumentValue("@id", existingOrder.id);
    final Query query2 = em.unwrap(JpaEntityManager.class).createQuery(interaction, Order.class);
    @SuppressWarnings("unchecked") final List<Object> result2 = query2.getResultList();
    assertEquals(String.format("Expected result of size 1, got %d", result2.size()), 1, result2.size());
    assertTrue(String.format("Result is not instance of Order but %s", result2.get(0).getClass().getSimpleName()), (result2.get(0) instanceof Order));
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.nosql.Order) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) DataRecord(org.eclipse.persistence.sessions.DataRecord) XMLInteraction(org.eclipse.persistence.eis.interactions.XMLInteraction) Test(org.junit.Test)

Example 3 with MappedInteraction

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

the class MongoPlatform method initializeDefaultQueries.

/**
 * INTERNAL:
 * Allow the platform to initialize the CRUD queries to defaults.
 * Configure the CRUD operations using GET/PUT and DELETE.
 */
@Override
public void initializeDefaultQueries(DescriptorQueryManager queryManager, AbstractSession session) {
    // Insert
    if (!queryManager.hasInsertQuery()) {
        EISInteraction call = new MappedInteraction();
        call.setProperty(MongoPlatform.OPERATION, MongoOperation.INSERT);
        call.setProperty(MongoPlatform.COLLECTION, ((EISDescriptor) queryManager.getDescriptor()).getDataTypeName());
        queryManager.setInsertCall(call);
    }
    // Update
    if (!queryManager.hasUpdateQuery()) {
        EISInteraction call = new MappedInteraction();
        call.setProperty(MongoPlatform.OPERATION, MongoOperation.UPDATE);
        call.setProperty(MongoPlatform.COLLECTION, ((EISDescriptor) queryManager.getDescriptor()).getDataTypeName());
        queryManager.setUpdateCall(call);
    }
    // Read
    if (!queryManager.hasReadObjectQuery()) {
        MappedInteraction call = new MappedInteraction();
        call.setProperty(MongoPlatform.OPERATION, MongoOperation.FIND);
        call.setProperty(MongoPlatform.COLLECTION, ((EISDescriptor) queryManager.getDescriptor()).getDataTypeName());
        for (DatabaseField field : queryManager.getDescriptor().getPrimaryKeyFields()) {
            call.addArgument(field.getName());
        }
        queryManager.setReadObjectCall(call);
    }
    // Delete
    if (!queryManager.hasDeleteQuery()) {
        MappedInteraction call = new MappedInteraction();
        call.setProperty(MongoPlatform.OPERATION, MongoOperation.REMOVE);
        call.setProperty(MongoPlatform.COLLECTION, ((EISDescriptor) queryManager.getDescriptor()).getDataTypeName());
        for (DatabaseField field : queryManager.getDescriptor().getPrimaryKeyFields()) {
            call.addArgument(field.getName());
        }
        queryManager.setDeleteCall(call);
    }
}
Also used : MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) EISInteraction(org.eclipse.persistence.eis.interactions.EISInteraction) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 4 with MappedInteraction

use of org.eclipse.persistence.eis.interactions.MappedInteraction 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 5 with MappedInteraction

use of org.eclipse.persistence.eis.interactions.MappedInteraction 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

MappedInteraction (org.eclipse.persistence.eis.interactions.MappedInteraction)9 DataRecord (org.eclipse.persistence.sessions.DataRecord)5 Query (jakarta.persistence.Query)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 List (java.util.List)3 XMLInteraction (org.eclipse.persistence.eis.interactions.XMLInteraction)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 Test (org.junit.Test)3 EntityManager (jakarta.persistence.EntityManager)2 TypedQuery (jakarta.persistence.TypedQuery)2 ArrayList (java.util.ArrayList)2 EISInteraction (org.eclipse.persistence.eis.interactions.EISInteraction)2 QueryStringInteraction (org.eclipse.persistence.eis.interactions.QueryStringInteraction)2 Order (org.eclipse.persistence.testing.models.jpa.mongo.Order)2 MappedRecord (jakarta.resource.cci.MappedRecord)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 EISDescriptor (org.eclipse.persistence.eis.EISDescriptor)1 EISException (org.eclipse.persistence.eis.EISException)1 EISMappedRecord (org.eclipse.persistence.eis.EISMappedRecord)1 Expression (org.eclipse.persistence.expressions.Expression)1