【发布时间】:2020-05-04 20:27:49
【问题描述】:
我有一堂课:
@Entity
@Table(name = "restaurants")
public class Restaurant extends AbstractNamedEntity implements Serializable {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "restaurant")
private Set<Meal> meals = Collections.emptySet();
//other fields, getters, setters, constructors
}
我正在使用 Spring Data 获取数据:
@Repository
public interface RestaurantRepository extends CrudRepository<Restaurant, Integer> {
}
我有 REST 控制器,它产生 JSON 数据:
@RestController
@RequestMapping(value = RestaurantController.REST_URL, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
public class RestaurantController {
static final String REST_URL = "/restaurants";
@Autowired
private RestaurantRepository repository;
@GetMapping("{id}")
public List<Restaurant> getOne(@PathVariable Integer id) {
return repository.findById(id);
}
}
如何避免包含那些 LAZY 数据(一组膳食)以将它们发送到 SQL 请求? 据我所知,我需要编写一个自定义 JacksonObjectMapper,但我不知道该怎么做
【问题讨论】:
标签: java hibernate spring-boot spring-data