Ali Yucel Akgul
Ali Yucel Akgul

Reputation: 1123

GSON serialization of parent class

I came across with a problem that pulls my hair off. I would like to serialize a class to json by using GSON library but it doesn't seem to work. I have an 'Parkings' class (entity) that is mapped to its upper class 'Floor' with one to many relationship. Whenever I try to serialize Parkings class I get the following result:

[{"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}}]

Then I have excluded '_persistence_floorId_vh' and '_persistence_fetchGroup' fields by a custom ExclusionStrategy. the result became:

[{"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"}]

which seems almost OK. I just want to add FloorID for each json object too. Couldn't figure out how to implement that. I would be greatly appriciated for any help.

Edit: my entity classes are:

    @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 + " ]";
    }    
}

And parking class is:

@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 + " ]";
    }   
}

Upvotes: 0

Views: 1330

Answers (1)

ckarabulut
ckarabulut

Reputation: 871

Your serialized json object doesn't seem to be serialized properly because of a problem in the entity definition.

   {
      "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
      }
   }

You should remove optional = false of ManyToOne annotation in the below code to get a properly serialized object .

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

Then you can implement an ExclusionStrategy to interrupt further serialization in the floor entity. You can do it as below;

    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;
    }

Upvotes: 1

Related Questions