简体   繁体   English

Hibernate:findAll 方法中的多对一获取问题

[英]Hibernate: Many-to-One fetching issues in findAll method

I am having problems with Hibernate when fetching a list of objects with a ManyToOne relation.在获取具有多对一关系的对象列表时,我在使用 Hibernate 时遇到了问题。 Below you can find the Entity and the Repository file.您可以在下面找到实体和存储库文件。 You can see that the Portofolio object has a benchmark which is a Portfolio itself.你可以看到 Portofolio 对象有一个基准,它是一个 Portfolio 本身。

The Entity实体

@Entity
//@NamedEntityGraph(name = "Portfolio.benchmark", attributeNodes = @NamedAttributeNode("benchmark"))
@Table(name = "PORTFOLIOS")
public class Portfolio {

    ...

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "BENCHMARK_ID", insertable = false, updatable = false)
    private Portfolio benchmark;

    @Column(name = "BENCHMARK_ID")
    private Long benchmarkId;

    ....

}

The Repository存储库

@Repository
public interface PortfolioRepository extends CrudRepository<Portfolio, Long> {

    //@EntityGraph(value = "Portfolio.benchmark", type = EntityGraph.EntityGraphType.LOAD)
    //@Query("select portfolio from Portfolio portfolio left join fetch portfolio.benchmark where upper(portfolio.userName) = upper(:userName) ")
    List<Portfolio> findAllByUserNameIgnoreCase(String userName);

}

The commented code are approaches that I tried before but did not work.注释代码是我之前尝试过但没有奏效的方法。

The Scenario情景

I have 3 portfolios with the following ids;我有 3 个具有以下 ID 的投资组合; let's say: 1, 2, 3比方说:1, 2, 3

Portfolio 1 has as benchmark the Portfolio id 2 and portfolio 2 has as benchmark the Portfolio id 3.投资组合 1 以投资组合 ID 2 作为基准,投资组合 2 以投资组合 ID 3 作为基准。

When executing the repository method findAllByUserNameIgnoreCase , I need to have a list of portfolios for that user, with the information of each benchmark.在执行存储库方法findAllByUserNameIgnoreCase ,我需要有一个该用户的投资组合列表,以及每个基准的信息。

The information for the Portfolio 1 is loaded properly, but for the porfolio 2, the benchmark is not loaded. Portfolio 1 的信息已正确加载,但对于 porfolio 2,未加载基准。

Here is a representation of the response:这是响应的表示形式:

(Portfolio@20661) = {portfolioId=1, benchmarkId=2, benchmark@20680 = {portfolioId=2, benchmarkId=3, benchmark = null}}
(Portfolio@20680) = Portfolio{portfolioId=2, benchmarkId=3, benchmark = null}
(Portfolio@20698) = Portfolio{portfolioId=3, benchmarkId=null, benchmark = null}

As we can see the porfolio 2 has the Java object reference 20680. It is the same in the benchmark attribute of the portfolio 1, and it is correct.可以看到,投资组合2的Java对象引用为20680,在投资组合1的benchmark属性中也是一样的,是正确的。

I don't understand, why for the Portfolio 1, the benchmark relation was fetched, for the Portfolio 2, the benchmark was not.我不明白,为什么对于 Portfolio 1,获取了基准关系,对于 Portfolio 2,却没有获取基准关系。

I tried to configure EntityGraph but it did not have any effect.我尝试配置EntityGraph但它没有任何效果。

What am I doing wrong?我究竟做错了什么? How can I get all the benchmark references resolved when calling the findAll method?调用findAll方法时如何解决所有基准参考?

I found the problem, and tipically it is between the screen and the chair.我发现了问题,通常是在屏幕和椅子之间。

The problem was not in the entity nor the repository.问题不在实体也不在存储库中。 It was a mapper to DTO that was setting in propertly a relation to null.它是一个映射到 DTO 的映射器,它正确地设置了与 null 的关系。

I am sorry about this, and thank you so much for the attention you gave to my question.对此我深表歉意,非常感谢您对我的问题的关注。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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