将jsonString转换为Java对象时,Gson会忽略Java对象集

时间:2019-01-30 19:22:27

标签: java serialization set gson entity

我将json字符串发送到我的servlet,当我尝试使用gson将json转换为java对象时,它忽略或未正确转换,它忽略了java对象Set <>。我不明白这是什么问题。 collegeSpecialization集的大小显示为:1,仅获得1个对象的specificationizationName(工程名)。也为工程学创建了CollegeBranchSet。

{"collegeName":"Sri Krishnadevaraya College of Agricultural Sciences",
"collegeSpecialization":[ 
{"specializationName":"Engineering","CollegeBranchSet":[{"branchName":"ebranch","branchStrength":12}]},
{"specializationName":"Science","CollegeBranchSet":[{"branchName":"sbranch","branchStrength":34}]},
{"specializationName":"Humanities","CollegeBranchSet":[{"branchName":"hbranch","branchStrength":32}]},
{"specializationName":"Business Admin","CollegeBranchSet":[{"branchName":"bbranch","branchStrength":21}]},
{"specializationName":"Medicine & related","CollegeBranchSet":[{"branchName":"mbranch","branchStrength":54}]}
]}
public class Colleges implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "college_id")
private Integer collegeId;
@Column(name = "college_name")
private String collegeName;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "collegeId", cascade = CascadeType.ALL)
private Set<CollegeSpecialization> collegeSpecialization = new HashSet<CollegeSpecialization>(0);
}
public class CollegeSpecialization implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "spec_id")
private Integer specId;
@Size(max = 50)
@Column(name = "specialization_name")
private String specializationName;
@Column(name = "status")
private Short status;
@OneToMany(mappedBy = "specId",fetch =  FetchType.EAGER,cascade=CascadeType.ALL)
private Set<CollegeBranch> collegeBranchSet;
@JoinColumn(name = "college_id", referencedColumnName = "college_id")
@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private Colleges collegeId; 
}

public class CollegeBranch implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "college_branch_id")
private Integer collegeBranchId;
@Size(max = 50)
@Column(name = "branch_name")
private String branchName;
@Column(name = "branch_strength")
private Integer branchStrength;
@Column(name = "status")
private Short status;
@JoinColumn(name = "spec_id", referencedColumnName = "spec_id")
@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private CollegeSpecialization specId;
}

Servlet

Gson gson = new Gson();
Colleges colleges = gson.fromJson(jsonString, Colleges.class);  
colleges.getTempCollegeSpecialization().size() // size is one 
"collegeSpecialization":[ {"specializationName":"Engineering" upto  this only converting after that all ll be ignored

我要转换所有CollegeSpecialization对象集和所有CollegeBranchSet对象集 注意:我也尝试过杰克逊(对象映射器),并且结果相同...

0 个答案:

没有答案