如何获取Hibernate对象并将其映射到JSON

时间:2014-04-14 21:47:25

标签: java json spring hibernate jackson

我有问题。当我试图获得JSON Employee对象时,我无法看到一个非常重要的字段(company)。例如,我需要得到这样的对象:

{
    "name": "Jack",
    "company": {
        "name": "Microsoft",
        "id": 4
    },
    "id": 1
}

但相反,我没有公司就得到了一个对象(错误!):

{
    "name": "Bill",
    "id": 2
}

同时我想获得公司的json对象:

{
    "name": "Microsoft",
    "employees": [
        {
            "name": "Jack",
            "id": 1
        },
        {
            "name": "Frank",
            "id": 3
        }
    ],
    "id": 4
}

公司对象没问题。当我得到它时,它看起来很完美。问题出在Employee对象中。

这是我的实体类

@Entity
@Table(name = "company")
public class Company implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column
    private String name;

    @OneToMany(mappedBy = "company")
    @Cascade(value = CascadeType.ALL)
    @JsonManagedReference
    private Collection<Employee> employees;

我的员工班

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;

    @Column
    String name;

    @ManyToOne()
    @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @JsonBackReference
    private Company company;

我认为问题出在@Json [SomeCondition]注释中。有人可以帮我修复代码吗?我需要像这样使用companym来获取Employee对象:

{
    "name": "Jack",
    "company": {
        "name": "Microsoft",
        "id": 4
    },
    "id": 1
}

UPD:

这是我的控制器:

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping(method = {RequestMethod.GET}, produces = "application/json")
    public @ResponseBody Collection<Employee> getEmployees() {
        return employeeService.getEmployees();
    }

    @RequestMapping(value = "/{id}", method = {RequestMethod.GET, RequestMethod.PUT},
                    produces = "application/json")
    public @ResponseBody Employee getEmployee(@PathVariable("id") Integer id) {
        return employeeService.getEmployee(id);
    }

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json",
                    headers = "content-type=application/json")
    public String addEmployee(@RequestBody Employee employee) {
        employeeService.addEmployee(employee);
        return "redirect:/employee";
    }

    @RequestMapping(method = RequestMethod.PUT, consumes = "application/json",
                    headers = "content-type=application/json")
    public String updateEmployee(@RequestBody Employee employee) {
        employeeService.updateEmployee(employee);
        return "redirect:/employee/" + employee.getId();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id) {
        employeeService.deleteEmployee(id);
        return "redirect:/employee";
    }
}

1 个答案:

答案 0 :(得分:0)

是休眠时获取EAGER和LAZY的问题。小心这两个概念。在实现它之前阅读它。 在公司类中定义关系时添加fetch = FetchType.EAGER。

 @OneToMany(mappedBy = "company",fetch=FetchType.EAGER)
     @Cascade(value = CascadeType.ALL)
     @JsonManagedReference
    private Collection<Employee> employees;