我遇到了一个让我脱掉头发的问题。我想通过使用GSON库将类序列化为json,但它似乎不起作用。我有一个'Parkings'类(实体),它被映射到具有一对多关系的上层“Floor”。每当我尝试序列化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}}]
然后我通过自定义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。无法弄清楚如何实现这一点。如果有任何帮助,我会非常感激。
编辑:我的实体类是:
@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 + " ]";
}
}
停车课程是:
@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 + " ]";
}
}
答案 0 :(得分:1)
由于实体定义中存在问题,您的序列化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来中断floor实体中的进一步序列化。你可以这样做;
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;
}