0

I have a MongoDB collection and document with an embedded array of objects: 'qtyContents'. Populated with test String data for the PoC:

id:5aa2c7b4aaa32bcb1d7cfc93 ean: "05052319711639" qtyContents : Array 0 : Object quantity : "1.1" totalQuantity : "1.2" quantityUom : "1.3" netContents : "1.4" avgMeasure : "1.5" 1 : Object quantity : "2.1" totalQuantity : "2.2" quantityUom : "2.3" netContents : "2.4" avgMeasure : "2.5"

My Entity is:

@Entity
@Indexed
@Table(name = "foodsCosmeticsMedicines")
public class FoodsCosmeticsMedicines implements Serializable {

    @ElementCollection
    private List<QtyContents> qtyContentsList;

    //setters & getters
}

and for 'QtyContents':

@Embeddable
public class QtyContents implements Serializable {

    private String quantity;
    private String totalQuantity;
    private String quantityUom;
    private String netContents;
    private String avgMeasure;

    //setters & getters
}

When I run my unit test I get:

09:44:18,762 INFO [com.notifywell.controller.NOTiFYwellController] (default task-56) >>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 09:44:18,764 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56)

getAllFoodsCosmeticsMedicinesJSON = 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) >>>>> getAllFoodsCosmeticsMedicinesJSON id = 5aa2c7b4aaa32bcb1d7cfc93 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) >>>>> getAllFoodsCosmeticsMedicinesJSON description = 09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) >>>>> getAllFoodsCosmeticsMedicinesJSON qtyContents = 0 09:44:18,802 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) [ { "id": "5aa2c7b4aaa32bcb1d7cfc93", "ean": "05052319711639", "description": "" } ]

I get the 'FoodsCosmeticsMedicines' collection of one:

09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) >>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1

but the 'qtyContents' array is empty.

09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-56) >>>>> getAllFoodsCosmeticsMedicinesJSON qtyContents = 0

Where it should have two documents.

Any idea what I'm doing wrong with the annotations for the array/collection?

NOTiFY
  • 1,255
  • 1
  • 20
  • 36
  • Can you share the test case somewhere? It would be easier for us to check what's going on. I don't see anything obviously wrong in the example – Davide D'Alto Mar 12 '18 at 21:51
  • Can you give me a location I can put a zip IntelliJ Project? – NOTiFY Mar 12 '18 at 22:45
  • We usually create projects on github so that we can clone them locally. Maybe you can upload it on our JIRA? https://hibernate.atlassian.net/projects/OGM/issues/OGM-1418?filter=allissues On the left side there is a '+' to create new issues. Sharing a link to dropbox, Google Drive or whatever you use works as well – Davide D'Alto Mar 13 '18 at 10:10

1 Answers1

0

The unit test just (using HTTP GET) calls a method in a Controller (an @model annotated POJO) exposed as a web service.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/get-foods-cosmetics-medicines")

The Controller has an EJB @inject'ed into it using CDI. The Controller calls a method in the injected EJB which queries the MongoDB database and collection and returns the result as JSON. Printing off the values of my embedded entities using the logger.

EJB method:

   /**
     * @return String
     */
    public String getAllFoodsCosmeticsMedicinesJSON() {
        logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON = ");

        Query query = entityManager.createQuery("FROM FoodsCosmeticsMedicines f");
        List<com.notifywell.entity.FoodsCosmeticsMedicines> foodsCosmeticsMedicinesList = query.getResultList();
        logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = " + foodsCosmeticsMedicinesList.size());

        for (FoodsCosmeticsMedicines foodsCosmeticsMedicines : foodsCosmeticsMedicinesList) {
            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON id = " + foodsCosmeticsMedicines.getId());
            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON ean = " + foodsCosmeticsMedicines.getEan());
            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON description = " + foodsCosmeticsMedicines.getDescription());

            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON getQtyContentsList = " + foodsCosmeticsMedicines.getQtyContentsList());
            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON getProductCharacteristics getProductCharacteristics = " + foodsCosmeticsMedicines.getProductCharacteristics().getIsFood());
            logger.info(">>>>> getAllFoodsCosmeticsMedicinesJSON getLifestyle getLifestyle = " + foodsCosmeticsMedicines.getLifestyle().getName());
        }

        Gson gson = getGsonBuilder().create();
        // deserialize
        String json = gson.toJson(foodsCosmeticsMedicinesList);

        logger.info(json);

        return json;
    }

My persistence.xml

<persistence-unit name="nOTiFYwellMongoDBPersistenceUnit" transaction-type="JTA">

        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <properties>
            <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2"/>
            <property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.8"/>

            <!-- <property name="hibernate.transaction.jta.platform" value="JBossTS"/> -->
            <!-- <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAS"/> -->
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
            <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
            <property name="hibernate.ogm.datastore.grid_dialect" value="org.hibernate.ogm.datastore.mongodb.MongoDBDialect"/>
            <property name="hibernate.ogm.datastore.database" value="notifyWellDB"/>
            <property name="hibernate.ogm.mongodb.host" value="127.0.0.1"/>
        </properties>
    </persistence-unit>

The output:

22:25:36,538 INFO [com.notifywell.controller.NOTiFYwellController] (default task-4) >>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 22:25:36,541 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON id = 5aa6bc4340cf3a7178094a8f 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4)

getAllFoodsCosmeticsMedicinesJSON description = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON getQtyContentsList = [] 22:25:36,552 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) >>>>> getAllFoodsCosmeticsMedicinesJSON getProductCharacteristics getProductCharacteristics = 22:25:36,552 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) getAllFoodsCosmeticsMedicinesJSON getLifestyle getLifestyle = Lifestyle 22:25:36,586 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (default task-4) [ { "id": "5aa6bc4340cf3a7178094a8f", "ean": "05052319711639", "description": "" } ]

You can see that the line in bold shows the empty array.

NOTiFY
  • 1,255
  • 1
  • 20
  • 36
  • This space is supposed to be used for the answers, if you have additional comments you should add them to the question. – Davide D'Alto Mar 13 '18 at 15:00