父级的 GSON 序列化 class

GSON serialization of parent class

我遇到了一个让我头疼的问题。我想使用 GSON 库将 class 序列化为 json,但它似乎不起作用。我有一个 'Parkings' class (实体)映射到其上层 class 'Floor' 具有一对多关系。每当我尝试序列化 Parkings class 时,我都会得到以下结果:

[{"id":1,"parkingAreaId":0,"info":"Parking 1 for Floor 1","_persistence_floorId_vh":{"sourceAttributeName":"floorId","isInstantiated":false,"row":{"asd.parkings.ID":1,"asd.parkings.INFO":"Parking 1 for Floor 1","asd.parkings.PARKING_AREA_ID":0,"asd.parkings.FLOOR_ID":1},"isCoordinatedWithProperty":false}},{"id":2,"parkingAreaId":0,"info":"Parking 1 for Floor 1","_persistence_floorId_vh":{"sourceAttributeName":"floorId","isInstantiated":false,"row":{"asd.parkings.ID":2,"asd.parkings.INFO":"Parking 1 for Floor 1","asd.parkings.PARKING_AREA_ID":0,"asd.parkings.FLOOR_ID":1},"isCoordinatedWithProperty":false}},{"id":3,"parkingAreaId":1,"info":"Kat 2 Parking Area 1","_persistence_floorId_vh":{"sourceAttributeName":"floorId","isInstantiated":false,"row":{"asd.parkings.ID":3,"asd.parkings.INFO":"Kat 2 Parking Area 1","asd.parkings.PARKING_AREA_ID":1,"asd.FLOOR_ID":2},"isCoordinatedWithProperty":false}}]

然后我通过自定义 ExclusionStrategy 排除了“_persistence_floorId_vh”和“_persistence_fetchGroup”字段。结果变成了:

[{"id":1,"parkingAreaId":0,"info":"Parking 1 for Floor 1"},{"id":2,"parkingAreaId":0,"info":"Parking 1 for Floor 1"},{"id":3,"parkingAreaId":1,"info":"Kat 2 Parking Area 1"}]

这看起来差不多了。我也只想为每个 json 对象添加 FloorID。无法弄清楚如何实现它。如果有任何帮助,我将不胜感激。

编辑:我的实体 class 是:

    @Entity
@Table(name = "floor", catalog = "asd", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Floor.findAll", query = "SELECT f FROM Floor f"),
    @NamedQuery(name = "Floor.findById", query = "SELECT f FROM Floor f WHERE f.id = :id"),
    @NamedQuery(name = "Floor.findByFloorName", query = "SELECT f FROM Floor f WHERE f.floorName = :floorName"),
    @NamedQuery(name = "Floor.findByType", query = "SELECT f FROM Floor f WHERE f.type = :type")
})
public class Floor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "FLOOR_NAME", nullable = false, length = 255)
    private String floorName;
    @Basic(optional = false)
    @Lob
    @Column(name = "INFO", nullable = false, length = 65535)
    private String info;
    @Basic(optional = false)
    @Column(name = "TYPE", nullable = false)
    private int type;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "floorId", fetch = FetchType.LAZY)
    private List<Parkings> parkingsList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "floorId", fetch = FetchType.LAZY)
    private List<Areas> areasList;

    public Floor() {
    }

    public Floor(Integer id) {
        this.id = id;
    }

    public Floor(Integer id, String floorName, String info, int type) {
        this.id = id;
        this.floorName = floorName;
        this.info = info;
        this.type = type;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFloorName() {
        return floorName;
    }

    public void setFloorName(String floorName) {
        this.floorName = floorName;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @XmlTransient
    public List<Parkings> getParkingsList() {
        return parkingsList;
    }

    public void setParkingsList(List<Parkings> parkingsList) {
        this.parkingsList = parkingsList;
    }

    @XmlTransient
    public List<Areas> getAreasList() {
        return areasList;
    }

    public void setAreasList(List<Areas> areasList) {
        this.areasList = areasList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Floor)) {
            return false;
        }
        Floor other = (Floor) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Floor[ id=" + id + " ]";
    }    
}

停车class是:

@Entity
@Table(name = "parkings", catalog = "asd", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Parkings.findAll", query = "SELECT p FROM Parkings p"),
    @NamedQuery(name = "Parkings.findById", query = "SELECT p FROM Parkings p WHERE p.id = :id"),
    @NamedQuery(name = "Parkings.findByParkingAreaId", query = "SELECT p FROM Parkings p WHERE p.parkingAreaId = :parkingAreaId")})
public class Parkings implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "PARKING_AREA_ID", nullable = false)
    private int parkingAreaId;
    @Basic(optional = false)
    @Lob
    @Column(name = "INFO", nullable = false, length = 65535)
    private String info;
    @JoinColumn(name = "FLOOR_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Floor floorId;


    public Parkings() {
    }

    public Parkings(Integer id) {
        this.id = id;
    }

    public Parkings(Integer id, int parkingAreaId, String info) {
        this.id = id;
        this.parkingAreaId = parkingAreaId;
        this.info = info;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getParkingAreaId() {
        return parkingAreaId;
    }

    public void setParkingAreaId(int parkingAreaId) {
        this.parkingAreaId = parkingAreaId;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Floor getFloorId() {
        return floorId;
    }

    public void setFloorId(Floor floorId) {
        this.floorId = floorId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Parkings)) {
            return false;
        }
        Parkings other = (Parkings) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Parkings[ id=" + id + " ]";
    }   
}

您的序列化 json 对象似乎没有正确序列化,因为实体定义中存在问题。

   {
      "id":1,
      "parkingAreaId":0,
      "info":"Parking 1 for Floor 1",
      "_persistence_floorId_vh":{
         "sourceAttributeName":"floorId",
         "isInstantiated":false,
         "row":{
            "asd.parkings.ID":1,
            "asd.parkings.INFO":"Parking 1 for Floor 1",
            "asd.parkings.PARKING_AREA_ID":0,
            "asd.parkings.FLOOR_ID":1
         },
         "isCoordinatedWithProperty":false
      }
   }

您应该在下面的代码中删除 optional = false 的 ManyToOne 注释以获得正确序列化的对象。

@JoinColumn(name = "FLOOR_ID", referencedColumnName = "ID", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Floor floorId;

然后您可以实施一个 ExclusionStrategy 来中断地板实体中的进一步序列化。您可以按照下面的方式进行;

    String fieldName = f.getName();
    String className = f.getDeclaringClass().getSimpleName();
    if (className.equals("Floor")) {
        if (fieldName.equals("areasList")) {
            return true;
        }

        if (fieldName.equals("parkingsList")) {
            return true;
        }
        return false;
    }