gpt4 book ai didi

java - 为什么 Hibernate 会查询两次?

转载 作者:行者123 更新时间:2023-11-30 02:48:16 25 4
gpt4 key购买 nike

在测试 Spring 中 JPA 的实现时,我发现我的查询查询了两次而不是一次。

@Data
@Entity
@Table(name = "superfan_star")
public class Star implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private int id;
private String name;
private String nickname;
private String description;
private String thumbnail;
private String backgroundImage;
private Date created;
private Date updated;

@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "starId", referencedColumnName = "id", insertable = false, updatable = false)
private Set<Media> medias;
}

这是模型类。

@Service
public class SuperfanStarService
{
@Autowired
private StarRepository starRepository;

@PersistenceContext
private EntityManager em;

@Transactional
public List<Star> getStars()
{
QStar qStar = QStar.star;
QMedia qMedia = QMedia.media;

List<Star> stars =
new JPAQuery(em)
.from(qStar)
.where(qStar.id.eq(19))
.list(qStar);

return stars;
}
}

这是我的服务类别。

20160915 20:52:59.119 [http-nio-8080-exec-1] DEBUG j.sqlonly - org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) 9. select star0_.id as id1_2_, star0_.background_image as backgrou2_2_, star0_.created as created3_2_, star0_.description as descript4_2_, star0_.name as name5_2_, star0_.nickname as nickname6_2_, star0_.thumbnail as thumbnai7_2_, star0_.updated as updated8_2_ from superfan_star star0_ inner join superfan_media medias1_ on star0_.id=medias1_.star_id where star0_.id=19

20160915 20:52:59.173 [http-nio-8080-exec-1] DEBUG j.sqlonly - org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) 9. select medias0_.star_id as star_id11_2_0_, medias0_.id as id1_1_0_, medias0_.id as id1_1_1_, medias0_.created as created2_1_1_, medias0_.description as descript3_1_1_, medias0_.end_time as end_time4_1_1_, medias0_.is_approve as is_appro5_1_1_, medias0_.is_approved_final as is_appro6_1_1_, medias0_.is_pushed as is_pushe7_1_1_, medias0_.is_represent as is_repre8_1_1_, medias0_.length as length9_1_1_, medias0_.released as release10_1_1_, medias0_.star_id as star_id11_1_1_, medias0_.teleport_media_id as telepor12_1_1_, medias0_.thumbnail as thumbna13_1_1_, medias0_.title as title14_1_1_, medias0_.work_end as work_en15_1_1_, medias0_.work_start as work_st16_1_1_, medias0_.youtube_id as youtube17_1_1_, medias0_.youtube_title as youtube18_1_1_ from superfan_media medias0_ where medias0_.star_id=19

正如您所看到的,它查询了两次而不是一次,可能是因为反向更新?有没有办法让我的 JPA 模型只查询一次?

最佳答案

这按预期工作。第一个查询从数据库中获取 id = 19 的 Star 实体,第二个查询从以下位置获取该 Star 实体的链接 Media 实体:数据库。 (仔细查看SQL语句的日志,了解查询的内容)。

请注意,您在 Star 类的 medias 字段上指定了 FetchType.EAGER:

@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "starId", referencedColumnName = "id", insertable = false, updatable = false)
private Set<Media> medias;

急切获取意味着当您查询一个或多个Star对象时,Hibernate会立即获取链接的Media对象 - 与延迟获取相反,这意味着第二个查询不会立即执行,而是仅在必要时执行(当您访问 medias 成员变量时)。

关于java - 为什么 Hibernate 会查询两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com