[英]Error serializing Hibernate entity object list to JSON using Jackson
我已經從數據庫中獲取了Company
類型的對象的列表。 但是,當嘗試使用Jackson將序列化列表到JSON時,拋出錯誤
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.zerosolutions.repository.entity.CompanySector.companies, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.zerosolutions.repository.entity.Company["sector"]->com.zerosolutions.repository.entity.CompanySector["companies"])
公司:
@Entity
@Table(name = "company")
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "sector")
private CompanySector sector;
@OneToOne
@JoinColumn(name = "id")
private CompanyProfile profile;
@OneToOne
@JoinColumn(name = "id")
private CompanyAddress address;
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
private List<Job> jobs = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CompanySector getSector() {
return sector;
}
public void setSector(CompanySector sector) {
this.sector = sector;
}
public CompanyProfile getProfile() {
return profile;
}
public void setProfile(CompanyProfile profile) {
this.profile = profile;
}
public CompanyAddress getAddress() {
return address;
}
public void setAddress(CompanyAddress address) {
this.address = address;
}
public List<Job> getJobs() {
return jobs;
}
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}
}
公司部門:
@Entity
@Table(name = "sectors")
public class CompanySector {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="sector")
private String sector;
@OneToMany(mappedBy="sector", cascade=CascadeType.PERSIST)
private List<Company> companies = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSector() {
return sector;
}
public void setSector(String sector) {
this.sector = sector;
}
public List<Company> getCompanies() {
return companies;
}
}
轉換碼:
@RequestMapping(value = "/getCompanyList", method = RequestMethod.GET, produces={ "application/json"})
public @ResponseBody String getCompanyList() {
ObjectMapper mapper = new ObjectMapper();
try {
List<Company> companyList = companyServices.getCompanyList();
String companyListString = mapper.writeValueAsString(companyList); // this line throws error
return companyListString;
} catch (JsonProcessingException e) {
logger.error(e);
return null;
}
}
任何建議在這里可能有什么問題嗎?
正在獲取列表:
public List<Company> getCompanyList(){
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List<Company> companies = session.createCriteria(Company.class).list();
logger.debug(companies);
tx.commit();
System.out.println("companies fetched");
return companies;
} catch(Exception e){
logger.error("Exceptino thrown: " + e);
tx.rollback();
return null;
} finally{
session.close();
}
}
如果您知道每次檢索CompanySector
時都希望看到所有Companies
則將Companies
的字段映射更改為:
@OneToMany(fetch = FetchType.EAGER, mappedBy="sector", cascade=CascadeType.PERSIST)
private List<Company> companies = new ArrayList<>();
另一種方法使用Hibernate.initialize :
Hibernate.initialize(companySector.getCompany());
還可以看到此鏈接,它非常有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.