【问题标题】:JBoss WildFly 11, Hibernate ORM 5.2, OGM 5.3 and MongoDB - zero length array of ObjectsJBoss WildFly 11、Hibernate ORM 5.2、OGM 5.3 和 MongoDB - 零长度的对象数组
【发布时间】:2018-03-12 09:53:13
【问题描述】:

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

id:5aa2c7b4aaa32bcb1d7cfc93 ean: "05052319711639" qtyContents : 数组 0:对象数量:“1.1” totalQuantity:“1.2”数量Uom:“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 信息 [com.notifywell.controller.NOTiFYwellController] (默认任务 56)>>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 09:44:18,764 信息 [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 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 56) getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 09:44:18,771 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务 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", “伊恩”:“05052319711639”, “描述”:“”}]

我得到了一个“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

它应该有两个文件。

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

【问题讨论】:

  • 你能在某处分享测试用例吗?我们更容易检查发生了什么。我在示例中没有看到任何明显错误
  • 你能给我一个可以放 zip IntelliJ 项目的位置吗?
  • 我们通常在github上创建项目,这样我们就可以将它们克隆到本地。也许你可以把它上传到我们的 JIRA 上? hibernate.atlassian.net/projects/OGM/issues/… 在左侧有一个“+”来创建新问题。分享到 Dropbox、Google Drive 或您使用的任何东西的链接也很有效

标签: mongodb orm hibernate-ogm wildfly-11


【解决方案1】:

单元测试只是(使用 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 信息 [com.notifywell.controller.NOTiFYwellController] (默认任务 4)>>>>> NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 22:25:36,541 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON = 22:25:36,551 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 22:25:36,551 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON id = 5aa6bc4340cf3a7​​178094a8f 22:25:36,551 信息 [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务 4)>>>>> getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 22:25:36,551 INFO [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 获取产品特征 获取产品特征 = 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", “伊恩”:“05052319711639”, “描述”:“”}]

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

【讨论】:

  • 这个空间应该是用来回答问题的,如果你有额外的 cmets 你应该将它们添加到问题中。
猜你喜欢
  • 2018-08-11
  • 2018-08-10
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 2023-03-17
相关资源
最近更新 更多