[英]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. 我想通过使用GSON库将一个类序列化为json,但是它似乎不起作用。 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: 每当我尝试序列化Parkings类时,都会得到以下结果:
[{"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. 然后,我通过自定义ExclusionStrategy排除了“ _persistence_floorId_vh”和“ _persistence_fetchGroup”字段。 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. 我也只想为每个json对象添加FloorID。 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 + " ]";
}
}
Your serialized json object doesn't seem to be serialized properly because of a problem in the entity definition. 由于实体定义中的问题,您的序列化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
}
}
You should remove optional = false
of ManyToOne annotation in the below code to get a properly serialized object . 您应该在下面的代码中删除ManyToOne批注的optional = false
以获得正确序列化的对象。
@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. 然后,您可以实施ExclusionStrategy来中断发言权实体中的进一步序列化。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.