使用dropwizard 0.9.2和dropwizard-hibernate 0.9.2,我試圖檢索由@OneToMany
與hibernate映射的集合。不幸的是我得到這個異常:Dropwizard未能懶洋洋地初始化一個角色集合
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily
initialize a collection of role: com.ng.Computer.parts, could not
initialize proxy - no Session
我不明白爲什麼會發生(休眠會話時傑克遜試圖序列化對象關閉),但應該不是庫傑克遜的數據類型,hibernate4(其是dropwizard-hibernate的依賴項)自動爲我處理這個問題(重新打開會話並實現集合)?如果不是這個庫在做什麼以及大多數dropwizard應用程序如何解決這個問題(EAGER抓取不是一個選項)?
的pom.xml
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
<version>0.9.2</version>
</dependency>
模型
@Entity
public class Computer {
@Id
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private String name;
@JsonIgnore
@OneToMany
public List<Part> parts;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Part> getParts() {
return parts;
}
public void setParts(List<Part> parts) {
this.parts = parts;
}
}
@Entity
public class Part {
@Id
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
資源:
@GET
@UnitOfWork
@Timed
public Response list() {
Computer computer = computerDAO.findById(1L);
Response response = Response.ok(computer.getParts()).build();
return response;
}
DAO:
public class ComputerDAOImpl extends AbstractDAO<Computer> implements
ComputerDAO{
public ComputerDAOImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}
public Computer findById(Long computationId) {
Computer computer = get(computationId);
return computer;
}
}