正确使用实体和DTO在Restful Web服务中提供Json [英] Correct usage of Entity AND DTO to provide Json in Restful web service

查看:378
本文介绍了正确使用实体和DTO在Restful Web服务中提供Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多文章指出,使用DTOs对于JPA / hibernate不是必需的

使用视图模式中的打开会话,避免无法读取数据的问题.Hibernate使开发人员免于编写繁琐的数据传输对象(DTO)...
上述行来自 https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/best-practices .html



另外在文章由SO成员Bohzo我读DTO's很少需要

即使在 articles against exposing entities that does not need to have DTO when Entities do没有任何行为(当他们是PO JOs)与贫血域模型一样



让我们说有一个实体类

  class Department {
List< Employee>员工//延迟加载的集合

集合中的每个对象都包含另一个延迟加载的集合

  class Employee {
List< Account>账户

有一个getDepartment()方法
,一个宁静的服务使用它来提供JSON信息部门。



可能的解决方案是

解决方案1)根据hibernate文档打开和关闭每个请求的hibernate会话(这是控制器中最上面的方法是事务性的?)或更好的使用Spring的 OpenSessionInViewFilter 按照 SO帖子



为什么不能休眠重新打开会话并获取懒惰加载的对象,而不是抛出一个异常?有没有办法用JPA / hibernate配置这个?



解决方案2)同样在hibernate doc中另一个wa y是有一个组装阶段。究竟是什么意思?
将getDepartment API分解成DAO的不同API?



解决方案3)使用DTO's 即使使用DTO,持久层如何知道视图是否需要满载的Department。此
导致将API分解为getDepartmentOnly )getDepartmentWithEmployees()和其他人说是否获得百分之百的部门对象或其中的一部分
一个API分解成多个和一个映射到许多DTO的实体



解决方案4)正如在bohzo的分页视图文章中,避免延迟加载,并有查询获取有限的结果



解决方案2并解释hibernate文档中的目标是什么?

解决方案 / p>

  public Department getDepartmentWithEmp loyees(Long departmentId){
部门结果= em.find(Department.class,departmentId);
Hibernate.initialize(result.getEmployees());
返回结果;
}

或者:

<$公共部门getDepartmentWithEmployees(Long departmentId){
字符串查询=从部门中选择d连接fetch d.employees其中d.id =:departmentId; pre>
...
}

或...



基本上,取决于您需要获取必要的数据以提供代码可读性和可维护性与性能之间的平衡(不同API与获取数据量之间的平衡)。



这不是JPA / Hibernate特定的问题;您需要考虑使用任何其他持久性框架(或使用直接JDBC)。


There are many articles stating that using DTOs is not necessary with JPA/hibernate

Use the open session in view pattern, or a disciplined assembly phase to avoid problems with unfetched data.Hibernate frees the developer from writing tedious Data Transfer Objects (DTO)... The above lines are from https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/best-practices.html

Also In an article by a SO member Bohzo I read DTO's are rarely required

Even in articles against exposing entities state that there is no need of having a DTO when Entities do not have any behavior (when they are POJOs) as in the anemic domain model

Let's Say there is an Entity class

class Department{
    List<Employee> employees //lazily loaded collection 

Each object in the collection contains another lazily loaded collection

 class Employee{
    List<Account> accounts

There is a getDepartment() method which is used by a restful service to provide Json information of the Department.

The possible solutions are

Solution 1) As per hibernate documentation opening and closing the hibernate session per request (that is the uppermost method in controller is transactional?) or better using Spring's OpenSessionInViewFilter as per this SO post

Why can't hibernate re-open a session and fetch the lazily loaded object instead of throwing an exception?Is there a way to configure this with JPA/hibernate?

Solution 2) Again as in hibernate doc another way is to have an assembly phase.What exactly does it mean ? Break down the getDepartment API into different API's of the DAO ?

Solution 3)Use DTO's Even with DTO's how can the persistence layer know whether view needs a fully loaded Department or not.This leads to breaking the API into getDepartmentOnly() getDepartmentWithEmployees() and others saying whether to get 100% of the department object or a part of it One API broke down into many and one entity mapped to many DTO's

Solution 4) As in bohzo's article with paginated views avoid lazy loading and have queries to fetch limited results

Please correct Solution 2 and explain what is intended in the hibernate documentation?

解决方案

Assembly phase in the Hibernate documentation means:

public Department getDepartmentWithEmployees(Long departmentId) {
   Department result = em.find(Department.class, departmentId);
   Hibernate.initialize(result.getEmployees());
   return result;
}

Or:

public Department getDepartmentWithEmployees(Long departmentId) {
   String query = "select d from Department d join fetch d.employees where d.id = :departmentId";
   ...
}

Or ...

Basically, it's up to you to fetch the necessary data to provide the balance between code readability and maintainability vs performance (the number of different APIs vs the amount of data fetched).

This is not a JPA/Hibernate specific issue; you would need to consider this with any other persistence framework (or with direct JDBC) as well.

这篇关于正确使用实体和DTO在Restful Web服务中提供Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆