简体   繁体   English

有没有一种方法可以代表休眠的数据视图?

[英]Is there a way to represent a data view for hibernate?

I'm looking for something which would allow me to replicate something like Jackson's @JsonView functionality but on a database level. 我正在寻找可以让我在数据库级别复制像Jackson的@JsonView功能的东西。 Often I feel like I've hit the wall trying to balance between query performance and having all the necessary data returned from the API. 通常,我觉得自己在尝试平衡查询性能和从API返回所有必需的数据之间遇到了麻烦。

For example I have a class structure like: 例如,我有一个类似的类结构:

class Employee {
    String name;

    @ManyToOne(fetchType = EAGER)
    Company company;

    // A lot of other data
}

class Company {
    String name;

    // A lot of other data
}

Now I would like to display a list of Employees and additionally show the Company name of each employee in the table. 现在,我想显示一个员工列表,并在表中另外显示每个员工的公司名称。 Ideally in raw SQL world I would do something like 理想情况下,在原始SQL世界中,我会做类似的事情

select e.name, c.name from Employees as e left join Companies as c on e.companyId = c.id

But what instead happens is that hibernate queries the whole company data as well including other relations and their relations which often makes it really slow. 但是相反,发生的情况是休眠状态会查询整个公司的数据以及包括其他关系及其关系在内的数据,这通常会使其变得非常缓慢。 Lazy loading is also slower and doesn't work when you need to return the queried data from a controller. 延迟加载也较慢,并且当您需要从控制器返回查询的数据时不起作用。

Maybe there is something which would allow me to declare a data view in a manner like: 也许有些东西可以让我以如下方式声明数据视图:

class EmployeeListView {
    String name = Path.to("Employee.name");
    String companyName = Path.to("Employee.company.name")
}

?

对于任何关心的人, @NamedEntityGraph都是答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM