简体   繁体   中英

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. 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.

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

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都是答案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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