限制空数据会导致在JPA

时间:2018-10-25 07:03:23

标签: json spring-boot null spring-data-jpa one-to-many

实体1 PriceListDuration.java

package com.Pricing.Pricing_App.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"priceListId","statusCode","startDate","endDate","orgId"})


@Entity
@Table(name = "qp_price_lists_all_b_AV")
public class PriceListDuration {
    @Id
    @Column(name = "PRICE_LIST_ID") 
    private String priceListId; 

    @Column(name = "status_code")   
    private String statusCode; 

    @Column(name = "start_date")    
    private String startDate;

    @Column(name = "end_Date")  
    private String endDate;

    @Column(name = "org_id")    
    private String orgId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "PRICE_LIST_ID", referencedColumnName = "PRICE_LIST_ID")
    private PriceListDetail pricelistdetails;

    public PriceListDetail getPricelistdetails() {
        return pricelistdetails;
    }

    public void setPricelistdetails(PriceListDetail pricelistdetails) {
        this.pricelistdetails = pricelistdetails;
    }

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "PRICE_LIST_ID", referencedColumnName = "PRICE_LIST_ID")
    private List<PriceListItem> priceListItems;

    public List<PriceListItem> getPriceListItems() {
        return priceListItems;
    }

    public void setPriceListItems(List<PriceListItem> priceListItems) {
        this.priceListItems = priceListItems;
    }

    public String getPriceListId() {
        return priceListId;
    }

    public void setPriceListId(String priceListId) {
        this.priceListId = priceListId;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public String getOrgId() {
        return orgId;
    }

    public void setOrgId(String orgId) {
        this.orgId = orgId;
    }
}

实体2:PriceListItem.java

package com.Pricing.Pricing_App.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"pricingUOMCode","lineTypeCode","primaryPricingUOMFlag","priceListId","itemId","priceListItemId"})


@Entity
@Table(name = "QP_PRICE_LIST_ITEMS_AV")
public class PriceListItem implements Serializable {

    private static final long serialVersionUID = 1L;
    public PriceListItem() {
        super();
    }

    @Column(name = "price_list_item_id")    
    private String priceListItemId;

    @Column(name = "pricing_uom_code")  
    private String pricingUOMCode; 

    @Column(name = "line_type_code")    
    private String lineTypeCode; 

    @Column(name = "primary_pricing_uom_flag")  
    private String primaryPricingUOMFlag;

    @Column(name = "price_list_id") 
    private String priceListId; 

    @Id
    @Column(name = "item_id")   
    private String itemId;

//  For 1-1 mapping
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "item_id", referencedColumnName = "inventory_item_id")
    private ItemDetail itemDetails;

    public ItemDetail getItemDetails() {
        return itemDetails;
    }

    public void setItemDetails(ItemDetail itemDetails) {
        this.itemDetails = itemDetails;
    }

    public String getPricingUOMCode() {
        return pricingUOMCode;
    }

    public void setPricingUOMCode(String pricingUOMCode) {
        this.pricingUOMCode = pricingUOMCode;
    }

    public String getLineTypeCode() {
        return lineTypeCode;
    }

    public void setLineTypeCode(String lineTypeCode) {
        this.lineTypeCode = lineTypeCode;
    }

    public String getPrimaryPricingUOMFlag() {
        return primaryPricingUOMFlag;
    }

    public void setPrimaryPricingUOMFlag(String primaryPricingUOMFlag) {
        this.primaryPricingUOMFlag = primaryPricingUOMFlag;
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

}

实体3:ItemDetail.java

package com.Pricing.Pricing_App.model;

import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder({"itemNumber","inventoryItemId","organizationId"})


@Entity
@Table(name = "egp_system_items_b_AV")
public class ItemDetail {
    @Id
    @Column(name = "inventory_item_id") 
    private String inventoryItemId; 

    @Column(name = "item_number")   
    private String itemNumber; 

    @Column(name = "organization_id")   
    private String organizationId;

    public String getItemNumber() {
        return itemNumber;
    }

    public void setItemNumber(String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getInventoryItemId() {
        return inventoryItemId;
    }

    public void setInventoryItemId(String inventoryItemId) {
        this.inventoryItemId = inventoryItemId;
    }

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }
}

我们有三个实体,其中Entity1是所有者,E1-E2具有1-M映射,E2-E3具有1-1映射。

My Controller:
package com.Pricing.Pricing_App.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.Pricing.Pricing_App.model.PriceListDuration;
import com.Pricing.Pricing_App.repository.PriceListDurationRepository;

@RestController
public class PriceListDurationController {

    @Autowired
    private PriceListDurationRepository priceListDurationRepository;

    @GetMapping(value="/getBypriceListId")
    public List<PriceListDuration> getBypriceListId(@RequestParam("priceListId") String priceListId) {
        return priceListDurationRepository.findBypriceListId(priceListId);
    }

}

这是JSON输出

[
    {
        "priceListId": "300002009417575",
        "statusCode": "APPROVED",
        "startDate": "1998-12-31 00:00:00",
        "endDate": null,
        "orgId": "300001096431127",
        "pricelistdetails": {
            "priceListId": "300002009417575",
            "name": "COMMERCIAL PRICELIST",
            "description": "COMMERCIAL PRICELIST",
            "language": "US"
        },
        "priceListItems": [
            {
                "pricingUOMCode": "zzx",
                "lineTypeCode": "ORA_BUY",
                "primaryPricingUOMFlag": "Y",
                "itemId": "100000763258696",
                "itemDetails": {
                    "itemNumber": "SAM-SOLO-P2-PT",
                    "inventoryItemId": "100000763258696",
                    "organizationId": "300000245868293"
                }
            },
            {
                "pricingUOMCode": "zzx",
                "lineTypeCode": "ORA_BUY",
                "primaryPricingUOMFlag": "Y",
                "itemId": "100000763259581",
                "itemDetails": null
            }
        ]
    }
]

如您所见,ItemDetails(来自实体3)为空。 我想限制那些ItemItems为null的priceListItems块。 findBypriceListId返回上面给出的JSON输出。 输出还具有“ itemDetails”:null。 我不希望任何结果为null的块。 预期的JSON应该没有

   {
        "pricingUOMCode": "zzx",
        "lineTypeCode": "ORA_BUY",
        "primaryPricingUOMFlag": "Y",
        "itemId": "100000763259581",
        "itemDetails": null
    }

似乎当前的实现正在做外部联接

1 个答案:

答案 0 :(得分:0)

最好的方法是在检索数据时限制数据并不是更好的方法。与其将数据保存到表中而不是在数据不为null时限制数据,不如只保存列。

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "item_id", referencedColumnName = "inventory_item_id") 
@NotNull
private ItemDetail itemDetails;