0

我有一个 MongoDB 集合和文档,其中包含一个嵌入的对象数组:'qtyContents'。填充了 PoC 的测试字符串数据:

id:5aa2c7b4aaa32bcb1d7cfc93 ean:“05052319711639” qtyContents:数组 0:对象数量:“1.1”totalQuantity:“1.2”quantityUom:“1.3” netContents:“1.4”avgMeasure:“1.5”1:对象数量:“2.1”totalQuantity:“ 2.2" quantityUom : "2.3" netContents : "2.4" avgMeasure : "2.5"

我的实体是:

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

    @ElementCollection
    private List<QtyContents> qtyContentsList;

    //setters & getters
}

对于“数量内容”:

@Embeddable
public class QtyContents implements Serializable {

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

    //setters & getters
}

当我运行单元测试时,我得到:

09:44:18,762 INFO [com.notifywell.controller.NOTiFYwellController](默认任务 56)>>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 09:44:18,764 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)

getAllFoodsCosmeticsMedicinesJSON = 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-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 描述 = 09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)>>>>>getAllFoodsCosmeticsMedicinesJSON qtyContents = 0 09:44:18,802 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)[{“id”:“5aa2c7b4aaa32bcb1d7cfc93”,“ean”:“05052319711639”,“description”:“” ]

我得到了一个“FoodsCosmeticsMedicines”系列:

09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)>>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1

但 'qtyContents' 数组为空。

09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56)>>>>> getAllFoodsCosmeticsMedicinesJSON qtyContents = 0

它应该有两个文件。

知道我对数组/集合的注释做错了什么吗?

4

1 回答 1

0

单元测试只是(使用 HTTP GET)调用作为 Web 服务公开的控制器(@model 注释的 POJO)中的方法。

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

控制器使用 CDI 将 EJB @inject 注入其中。控制器在注入的 EJB 中调用一个方法,该方法查询 MongoDB 数据库和集合并以 JSON 形式返回结果。使用记录器打印出我的嵌入式实体的值。

EJB方法:

   /**
     * @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;
    }

我的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>

输出:

22:25:36,538 INFO [com.notifywell.controller.NOTiFYwellController](默认任务 4)>>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 22:25:36,541 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 22:25:36,551 INFO [com. notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON id = 5aa6bc4340cf3a7​​178094a8f 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>> getAllFoodsCometics 05052319711639 22:25:36,551 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)

getAllFoodsCosmeticsMedicinesJSON 描述 = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4) >>>>> getAllFoodsCosmeticsMedicinesJSON getQtyContentsList = []22:25:36,552 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON getProductCharacteristics getProductCharacteristics = 22:25:36,552 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4 ) getAllFoodsCosmeticsMedicinesJSON getLifestyle getLifestyle = Lifestyle 22:25:36,586 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务 4) [ { "id": "5aa6bc4340cf3a7​​178094a8f", "ean": "05052319711639", "description": " } ]

您可以看到粗体线显示的是空数组。

于 2018-03-13T07:58:34.337 回答