我正在编写一个JPQL查询(使用Hibernate作为我的JPA提供程序)来获取实体Company
及其几个关联。这适用于我的“简单”ManyToMany关联,如下所示:
@Entity
@Table(name = "company")
@NamedQueries({
@NamedQuery(
name = "Company.profile.view.byId",
query = "SELECT c " +
"FROM Company AS c " +
"INNER JOIN FETCH c.city AS city " + <-- @ManyToOne
"LEFT JOIN FETCH c.acknowledgements " + <-- @ManyToMany
"LEFT JOIN FETCH c.industries " + <-- @ManyToMany
"WHERE c.id = :companyId"
)
})
public class Company { ... }
Hibernate创建一个查询来获取上面的内容,这很好。但是,我的Company
实体与存储在中间表中的数据之间也存在多对多关联,因此将其映射为三个实体之间的@OneToMany
和@ManyToOne
关联。
公司&lt; - CompanyService - &gt;服务
这些是我的代码中的三个实体。因此Company
实例具有CompanyService
个实体的集合,每个实体都与Service
实例有关系。我希望这是有道理的 - 否则请在问题的最后检查源代码。
现在,我想通过修改上述查询来获取给定公司的服务。我提前读过JPA不允许嵌套的fetch连接甚至是连接的别名,但是有些JPA提供者确实支持它,所以我试试了Hibernate。我试图修改查询:
@Entity
@Table(name = "company")
@NamedQueries({
@NamedQuery(
name = "Company.profile.view.byId",
query = "SELECT c " +
"FROM Company AS c " +
"INNER JOIN FETCH c.city AS city " +
"LEFT JOIN FETCH c.acknowledgements " +
"LEFT JOIN FETCH c.industries " +
"LEFT JOIN FETCH c.companyServices AS companyService " +
"LEFT JOIN FETCH companyService.service AS service " +
"WHERE c.id = :companyId"
)
})
public class Company { ... }
现在,Hibernate不是创建单个查询,而是创建以下查询:
#1
select ...
from company company0_
inner join City city1_ on company0_.postal_code = city1_.postal_code
[...]
left outer join company_service companyser6_ on company0_.id = companyser6_.company_id
left outer join service service7_ on companyser6_.service_id = service7_.id
where company0_.id = ?
#2
select ...
from company company0_
inner join City city1_ on company0_.postal_code = city1_.postal_code
where company0_.id = ?
#3
select service0_.id as id1_14_0_, service0_.default_description as default_2_14_0_, service0_.name as name3_14_0_
from service service0_
where service0_.id = ?
#4
select service0_.id as id1_14_0_, service0_.default_description as default_2_14_0_, service0_.name as name3_14_0_
from service service0_
where service0_.id = ?
查询#1
我遗漏了不相关的连接,因为这些都没关系。它似乎选择了我需要的所有数据,包括服务和中间实体数据(CompanyService
)。
查询#2
此查询只是从数据库及其City
中提取公司。急切地获取城市关联,但即使我将其更改为延迟提取,仍会生成查询。老实说,我不知道这个查询的用途。
查询#3 +查询#4
这些查询基于ID查找Service
个实例,可能基于查询#1中提取的服务ID。我没有看到这个查询的需要,因为这个数据已经在Query#1中提取了(正如Query#2中的数据已经在Query#1中获取)。此外,如果公司有许多服务,这种方法显然不能很好地扩展。
奇怪的是,似乎查询#1做了我想要的,或者至少它获取了我需要的数据。我只是不知道为什么Hibernate会创建查询#2,#3和#4。所以我有以下问题:
任何错误的指针或完成我想要的替代解决方案都将非常感激。下面是我的代码(排除了getter和setter)。非常感谢提前!
公司实体
@Entity
@Table(name = "company")
@NamedQueries({
@NamedQuery(
name = "Company.profile.view.byId",
query = "SELECT c " +
"FROM Company AS c " +
"INNER JOIN FETCH c.city AS city " +
"LEFT JOIN FETCH c.acknowledgements " +
"LEFT JOIN FETCH c.industries " +
"LEFT JOIN FETCH c.companyServices AS companyService " +
"LEFT JOIN FETCH companyService.service AS service " +
"WHERE c.id = :companyId"
)
})
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
// ...
@ManyToOne(fetch = FetchType.EAGER, targetEntity = City.class, optional = false)
@JoinColumn(name = "postal_code")
private City city;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "company_acknowledgement", joinColumns = @JoinColumn(name = "company_id"), inverseJoinColumns = @JoinColumn(name = "acknowledgement_id"))
private Set<Acknowledgement> acknowledgements;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "company_industry", joinColumns = @JoinColumn(name = "company_id"), inverseJoinColumns = @JoinColumn(name = "industry_id"))
private Set<Industry> industries;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
private Set<CompanyService> companyServices;
}
公司服务实体
@Entity
@Table(name = "company_service")
@IdClass(CompanyServicePK.class)
public class CompanyService implements Serializable {
@Id
@ManyToOne(targetEntity = Company.class)
@JoinColumn(name = "company_id")
private Company company;
@Id
@ManyToOne(targetEntity = Service.class)
@JoinColumn(name = "service_id")
private Service service;
@Column
private String description;
}
服务实体
@Entity
@Table(name = "service")
public class Service {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Column(length = 50, nullable = false)
private String name;
@Column(name = "default_description", nullable = false)
private String defaultDescription;
}
获取数据
public Company fetchTestCompany() {
TypedQuery<Company> query = this.getEntityManager().createNamedQuery("Company.profile.view.byId", Company.class);
query.setParameter("companyId", 123);
return query.getSingleResult();
}
答案 0 :(得分:1)
好吧,我好像想通了。通过在FetchType.LAZY
中将获取类型设置为CompanyService
,Hibernate停止生成所有基本上再次获取相同数据的冗余查询。以下是该实体的新版本:
@Entity
@Table(name = "company_service")
@IdClass(CompanyServicePK.class)
public class CompanyService implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Company.class)
@JoinColumn(name = "company_id")
private Company company;
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Service.class)
@JoinColumn(name = "service_id")
private Service service;
@Column
private String description;
}
JPQL查询保持不变。
但是,在我的Company
实体具有的关联数的特定情况下,我得到了大量重复数据,因此让Hibernate执行额外查询更有效。我通过从我的JPQL查询中删除两个连接提取并将我的查询代码更改为下面来完成此操作。
@Transactional
public Company fetchTestCompany() {
TypedQuery<Company> query = this.getEntityManager().createNamedQuery("Company.profile.view.byId", Company.class);
query.setParameter("companyId", 123);
try {
Company company = query.getSingleResult();
Hibernate.initialize(company.getCompanyServices());
return company;
} catch (NoResultException nre) {
return null;
}
}
通过初始化companyServices
关联,Hibernate执行另一个查询来获取服务。在我的特定用例中,这比通过一个查询获取大量冗余数据更好。
我希望这有助于某人。如果有人有任何更好的解决方案/改进,那么我当然会很高兴听到他们。
答案 1 :(得分:0)
根据您所写的内容,我会说嵌套提取不受支持。这是我对你的结果的理解:
CompanyService#company
(急切city
会产生inner join City
)CompanyService#service
我知道这不是一个答案,但它可以帮助您了解后台发生的事情。