【问题标题】:JPA Hibernate is creating temporary tables for a simple update queryJPA Hibernate 正在为简单的更新查询创建临时表
【发布时间】:2016-11-25 17:00:33
【问题描述】:

我在我的存储库中有一个简单的查询来增加给定帖子对象的帖子的 cmets 数量:

public interface PostsRepository extends PagingAndSortingRepository<Post, Long>{

//...

@Modifying
@Query("UPDATE Post p set p.numComments = p.numComments + 1 where p = :post")
void incrementNumComments(@Param("post") post obj);

//...
}

型号:

@Entity
@Table(name="posts")
@Inheritance(strategy= InheritanceType.JOINED)
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "post_id", updatable = false, nullable = false)
    @Access(AccessType.PROPERTY)
    private Long id;
}

不过,hibernate 似乎是在使用临时表来执行查询。以下是从日志中生成的查询:

Hibernate: insert into HT_posts select pos0_.post_id as post_id from posts pos0_ where pos0_.s_id=?
Hibernate: update posts set num_comments=num_comments+1 where (post_id) IN (select post_id from HT_posts)

有没有办法阻止 Hibernate 对此查询使用临时表?

谢谢

【问题讨论】:

  • 我认为原因在注释@Inheritance(strategy= InheritanceType.JOINED)
  • @Giovanni 我也怀疑过,但如果注释是罪魁祸首,我不确定如何纠正。

标签: java spring hibernate jpa


【解决方案1】:

您可以找到有关Multitable bulk operations 的详细信息。

正如@Giovanni 提到的,这是因为使用了InheritanceType.JOINED

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2013-07-01
    • 2013-07-10
    • 2022-11-22
    • 2013-03-26
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多