【问题标题】:String Boot query brings empty entity for existing data字符串引导查询为现有数据带来空实体
【发布时间】:2018-04-07 17:44:56
【问题描述】:

我有这个 Spring Boot 应用程序,我有一个配置文件实体,我有我的存储库。

问题是,当我通过电子邮件对某个配置文件进行第二次查询时,该对象返回非空,但其中没有数据,因此我无法比较该对象的数据。

我的仓库:

public interface ProfileRepository extends JpaRepository<Profile, Long> {

    Profile findById(Long id);

    @Query("select e from Profile e where e.email = ?1")
    Profile findByEmail(String email);
}

执行这些查询的代码:

Profile myProfile = this.profileRepository.findById(data.getIdMyProfile());
Profile hisProfile = this.profileRepository.findByEmail(data.getEmail()); // see this one on the image - here is the problem        
Card myCard = this.cardRepository.findById(data.getIdMyCard());

我意识到,如果我连续对 Profile 实体进行 2 次查询,就会发生这种情况,我反转查询以查看差异,第二个对象不为空,但根本没有数据。

知道为什么会出现这个问题吗?

【问题讨论】:

  • 尝试删除 @query 注释,因为您不需要任何自定义查询,只需简单的存储库检索。
  • 我确实删除了@Query 注释,同样的结果,我认为是别的,因为在另一种方法中查询适用于电子邮件。

标签: spring hibernate spring-boot


【解决方案1】:

我找到了解决方案,一个实体在它的一个属性上带有 .LAZY,它指向 Profile 实体(作为一个孩子),我改为 .EAGER问题解决了:

// The owner of this card
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
@JoinColumn(name = "id_profile")
private Profile profile;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2013-07-04
    • 2012-06-15
    • 1970-01-01
    • 2018-04-14
    • 2012-10-14
    • 1970-01-01
    相关资源
    最近更新 更多