我有一个包含嵌入式对象数组的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
}
和&#39; QtyContents&#39;:
@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)&gt;&gt;&gt;&gt;&gt; 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)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON id = 5aa2c7b4aaa32bcb1d7cfc93 09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-56) getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务-56)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON描述 = 09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-56)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON qtyContents = 0 09:44:18,802 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-56)[{ &#34; id&#34;:&#34; 5aa2c7b4aaa32bcb1d7cfc93&#34;, &#34; ean&#34;:&#34; 05052319711639&#34;, &#34;描述&#34;:&#34;&#34; }]
我得到了&#39; FoodsCosmeticsMedicines&#39;收集一个:
09:44:18,770 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-56)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1
但是&#39; qtyContents&#39;数组是空的。
09:44:18,771 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-56)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON qtyContents = 0
它应该有两个文件。
知道我对数组/集合的注释做错了吗?
答案 0 :(得分:0)
单元测试(使用HTTP GET)调用Controller中的方法(@model注释的POJO)作为Web服务公开。
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/get-foods-cosmetics-medicines")
Controller使用CDI将EJB @ inject注入其中。 Controller在注入的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)&gt;&gt;&gt;&gt;&gt; NOTiFYwellController getAllFoodsCosmeticsMedicinesJSON ..... 22:25:36,541 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-4)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-4)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON foodsCosmeticsMedicinesList = 1 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务-4)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON id = 5aa6bc4340cf3a7178094a8f 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-4)&gt;&gt;&gt;&gt;&gt; getAllFoodsCosmeticsMedicinesJSON ean = 05052319711639 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-4)
getAllFoodsCosmeticsMedicinesJSON description = 22:25:36,551 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB](默认任务-4) 的&GT;&GT;&GT;&GT;&GT; getAllFoodsCosmeticsMedicinesJSON getQtyContentsList = [] 22:25:36,552 INFO [com.notifywell.ejb.FoodsCosmeticsMedicinesEJB] (默认任务-4)&gt;&gt;&gt;&gt;&gt; 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)[{ &#34; id&#34;:&#34; 5aa6bc4340cf3a7178094a8f&#34;, &#34; ean&#34;:&#34; 05052319711639&#34;, &#34;描述&#34;:&#34;&#34; }]
您可以看到以粗体显示的行显示空数组。