如何设置带有注释的映射以获得比现在更少的查询?
如何设置带有注释的映射以获得比现在更少的查询? 我只需要读取数据。
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置要加入的获取类型,如 Hibernate 文档。
You need to set the fetch type to join as explained in the Hibernate documentation.