如何设置带有注释的映射以获得比现在更少的查询?

发布于 2024-12-29 11:33:25 字数 1645 浏览 0 评论 0原文

如何设置带有注释的映射以获得比现在更少的查询? 我只需要读取数据。

...
private Long id;
private String firstName;
private String lastName;
private Address address;
private Set<Office> offices = new HashSet<Office>();
private Map<Office, Position> positions = new HashMap<Office, Position>();
private Office office;
    ...
    @OneToOne
    @JoinColumn(name = "ADDRESS_ID")
    public Address getAddress() {
        return address;
    }

    @ManyToMany(fetch = FetchType.EAGER, targetEntity = Office.class)
    @JoinTable(name = "H_OFFICE_EMPLOYEE",
        joinColumns = {@JoinColumn(name = "EMPLOYEE_ID")},
        inverseJoinColumns = {@JoinColumn(name = "OFFICE_ID")})
    public Set getOffices() {
        return offices;
    }

    @ManyToMany(targetEntity = Position.class)
    @JoinTable(name = "H_OFFICE_EMPLOYEE",
        joinColumns = {@JoinColumn(name = "EMPLOYEE_ID")},
        inverseJoinColumns = {@JoinColumn(name = "POSITION_ID")})
    @MapKeyManyToMany(joinColumns = @JoinColumn(name = "OFFICE_ID"), targetEntity = Office.class)
    public Map getPositions() {
        return positions;
    }
    ...

当获得 1 名员工(以及他的地址、城市、国家、他工作的公司、每个办公室(他工作的地方)的员工数)时,我会在控制台中收到下一个查询:

Hibernate: select employee0_.EMPLOYEE_ID as...
Hibernate: select address0_.ADDRESS_ID as...
Hibernate: select offices0_.EMPLOYEE_ID as...
Hibernate: select positions0_.EMPLOYEE_ID as...
Hibernate: select employees0_.OFFICE_ID as...
Hibernate: select offices0_.EMPLOYEE_ID as...
Hibernate: select employees0_.OFFICE_ID as...
Hibernate: select positions0_.EMPLOYEE_ID as...

我必须在映射中更改哪些内容才能减少查询?

How can i set mapping with annotations to get less queries than now?
I need only reading data.

...
private Long id;
private String firstName;
private String lastName;
private Address address;
private Set<Office> offices = new HashSet<Office>();
private Map<Office, Position> positions = new HashMap<Office, Position>();
private Office office;
    ...
    @OneToOne
    @JoinColumn(name = "ADDRESS_ID")
    public Address getAddress() {
        return address;
    }

    @ManyToMany(fetch = FetchType.EAGER, targetEntity = Office.class)
    @JoinTable(name = "H_OFFICE_EMPLOYEE",
        joinColumns = {@JoinColumn(name = "EMPLOYEE_ID")},
        inverseJoinColumns = {@JoinColumn(name = "OFFICE_ID")})
    public Set getOffices() {
        return offices;
    }

    @ManyToMany(targetEntity = Position.class)
    @JoinTable(name = "H_OFFICE_EMPLOYEE",
        joinColumns = {@JoinColumn(name = "EMPLOYEE_ID")},
        inverseJoinColumns = {@JoinColumn(name = "POSITION_ID")})
    @MapKeyManyToMany(joinColumns = @JoinColumn(name = "OFFICE_ID"), targetEntity = Office.class)
    public Map getPositions() {
        return positions;
    }
    ...

I get next queries in console when get 1 employee (and his address,city,country,companies where he work, employees count in every office (where he working)):

Hibernate: select employee0_.EMPLOYEE_ID as...
Hibernate: select address0_.ADDRESS_ID as...
Hibernate: select offices0_.EMPLOYEE_ID as...
Hibernate: select positions0_.EMPLOYEE_ID as...
Hibernate: select employees0_.OFFICE_ID as...
Hibernate: select offices0_.EMPLOYEE_ID as...
Hibernate: select employees0_.OFFICE_ID as...
Hibernate: select positions0_.EMPLOYEE_ID as...

What i must change in my mapping to get less queries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-01-05 11:33:25

您需要设置要加入的获取类型,如 Hibernate 文档

You need to set the fetch type to join as explained in the Hibernate documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文