我将从结果开始。在下一张图片中,我将显示 company.userAccount 的结果内容。在数据库中,我只有两家公司分配给同一个用户,其中一家公司有 7 项服务。
@Entity
@Table(name = "user_account")
@Data
@Introspected
@JsonIgnoreProperties(value = {"id"})
public class UserAccount {
...
@OneToMany(mappedBy = "userAccount", fetch = FetchType.EAGER)
private List<Company> companies = new ArrayList<>();
}
@Entity
@Table(name = "company")
@Data
@Introspected
@ToString(exclude = {"userAccount"})
@JsonIgnoreProperties(value = {"id", "userAccount"})
public class Company {
...
@ManyToOne(optional = false)
@JoinColumn(name = "user_account_id", updatable = false, nullable = false)
private UserAccount userAccount;
}
@Entity
@Table(name = "service")
@Data
@Introspected
@ToString(exclude = {"company"})
@JsonIgnoreProperties(value= {"id", "company"})
public class Service {
...
@ManyToOne(optional = false)
@JoinColumn(name = "company_id", updatable = false, nullable = false)
private Company company;
}
@Transactional
public Service saveService(@Valid @Uuid UUID companyUuid, SaveServiceCommand command) {
Company company = companyRepository.getCompanyByUuid(companyUuid).orElseThrow(() -> new NoSuchElementException("unrecognized company"));
Service service = new Service();
service.setCompany(company);
service.setName(command.getName());
service.setUuid(UUID.randomUUID().toString());
return companyRepository.saveCompanyService(service);
}
@Transactional
public Service saveCompanyService(@Valid Service service) {
entityManager.persist(service);
return service;
}
生成的查询(简化)是
select * from user_account a left outer join company c on a.id=c.user_account_id left outer join service s on c.id=s.company_id;
这将产生
预期结果:我期望在 UserAccount 中只看到 2 家公司,其中一家公司有 6 项服务。为什么我会有这个结果?为什么同一个公司对象多次出现在列表中?我怎样才能避免这种情况?
我相信一种解决方案是将 @OneToMany 关系的获取类型更改为 LAZY (因为它们是默认的)(已经测试了该解决方案并且它有效),但是如果我需要这种类型的场景怎么办?
杨__羊羊
相关分类