使用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;
}
}