Spring 引导 - 展平 JSON 由 JPA 规范返回的用于导出到 excel 的实体

Spring Boot - Flatten JSON Entity returned by JPA specification for exporting to excel

我正在为库存管理创建一个 Spring 引导项目。我有一个名为 InwardInventory 的实体,它与另一个名为 InwardOutwardList 的实体具有一对多关系。我正在使用 JPA 规范来过滤 Entity InwardInventory,它工作正常。过滤后我收到的响应是

{
    "inwardInventory": {
        "content": [
            {
                
                "inwardid": 19497,
                "date": "2019-05-28",
                "vehicleNo": "TRUCK",
                "supplierSlipNo": "",
                "ourSlipNo": "",
                "inwardOutwardList": [
                    {
                        "entryid": 19499,
                        "product": {
                            "productName": "Cement",
                          },
                        "quantity": 100.0
                    },
                    {
                        "entryid": 19500,
                        "product": {
                            "productName": "Iron",
                          },
                        "quantity": 30.0
                    }
                ],
                "warehouse": {
                    "warehouseName": "war2"
                },
                "supplier": {
                    "name": "Bright Traders"
                }
            }
        ]
    }
}

现在,我想将此数据导出到 excel。所以,我需要将此响应扁平化为类似这样的内容。

{
    "inwardInventory": {
        "content": [
            {
                
                "inwardid": 19497,
                "date": "2019-05-28",
                "vehicleNo": "TRUCK",
                "supplierSlipNo": "",
                "ourSlipNo": "",
                "entryid": 19499,
                "productName": "Cement",
                "quantity": 100.0,
                "warehouseName": "war2",
                "name": "Bright Traders"
            },
            {
                
                "inwardid": 19497,
                "date": "2019-05-28",
                "vehicleNo": "TRUCK",
                "supplierSlipNo": "",
                "ourSlipNo": "",
                "entryid": 19500,
                "productName": "Iron",
                "quantity": 30.0,
                "warehouseName": "war2",
                "name": "Bright Traders"
            }
        ]
    }
}

我知道我可以通过迭代每个内部库存然后嵌套迭代每个产品并创建自定义 DAO 来做到这一点。但是,这似乎不是优化的解决方案。

我也不能将投影或本机查询与自定义 select 列一起使用,因为它们无法与 jpa 规范集成。有人可以建议我可以用来以最佳方式实现这一目标的最佳方法吗?

谢谢。

通过自定义序列化程序处理了这个问题。下面的示例代码

@Component
public class OutwardExportSerializer extends StdSerializer<OutwardInventoryExportDAO> 
{
    private static final long serialVersionUID = 1L;
    protected OutwardExportSerializer(Class<OutwardInventoryExportDAO> t) 
    {
        super(t);
    }
    protected OutwardExportSerializer() 
    {
        this(null);
    }
    @Override
    public void serialize(OutwardInventoryExportDAO value, JsonGenerator gen, SerializerProvider provider) throws IOException 
    {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        for(InwardOutwardList ioList:value.getInwardOutwardList())
        {
            System.out.println("Serialization starts for OutWardID"+value.getOutwardid());
            gen.writeStartObject();
            gen.writeNumberField("outwardid", value.getOutwardid());
            gen.writeStringField("date",df.format(value.getDate()));
            gen.writeStringField("purpose",value.getPurpose()!=null?value.getPurpose():"");
            gen.writeStringField("slipNo",value.getSlipNo()!=null?value.getSlipNo():"");
            gen.writeStringField("product",ioList.getProduct().getProductName()!=null?ioList.getProduct().getProductName():"");
            gen.writeStringField("measurement unit",ioList.getProduct().getMeasurementUnit()!=null?ioList.getProduct().getMeasurementUnit():"");
            gen.writeNumberField("quantity",ioList.getQuantity()!=null?ioList.getQuantity():0.0);
            gen.writeNumberField("closing stock",ioList.getClosingStock()!=null?ioList.getClosingStock():0.0);
            gen.writeStringField("warehouse",value.getWarehouse()!=null?value.getWarehouse():"");
            gen.writeStringField("contractor",value.getContractor()!=null?value.getContractor():"");
            gen.writeStringField("additionalInfo",value.getAdditionalInfo()!=null?value.getAdditionalInfo():"");
            gen.writeStringField("usageLocation",value.getUsageLocation()!=null?value.getUsageLocation():"");
            gen.writeStringField("usageArea",value.getUsageArea()!=null?value.getUsageArea():"");
            gen.writeEndObject();
            System.out.println("Serialization ends for OutWardID"+value.getOutwardid());
        }
        
    }
}