【问题标题】:JPA Repository delete by a list of a composed PKJPA Repository 按组合PK列表删除
【发布时间】:2015-06-04 00:05:13
【问题描述】:

我正在尝试通过命名约定(不使用@Query)对 JPA 存储库进行删除。

关键是我有一个@EmbeddedId,所以repo header 看起来像

public interface MatchPronosticsRepository extends JpaRepository<MyClass, MyID>

所以默认情况下我有一个方法delete(MyID id),但我需要像delete(Iterable&lt;? extends MyID&gt;) 这样的东西,这样我就可以一枪杀死一些物体。关于如何解决它的任何想法?没找到

谢谢!

编辑

我尝试添加void deleteByMatchPronosticPk (Iterable&lt;? extends MatchPronosticPK&gt; ids); 但结果很奇怪。似乎在做一个 Select 会很好,但使用 3 个字段和 3 个值,但只有 2 个占位符:S

Hibernate: 
    select
        matchprono0_.match_id as match3_4_,
        matchprono0_.isprimary as isprimar1_4_,
        matchprono0_.user_id as user4_4_,
        matchprono0_.match_result as match2_4_ 
    from
        match_pronostics matchprono0_ 
    where
        (
            matchprono0_.match_id, matchprono0_.isprimary, matchprono0_.user_id
        )=(
            ? , ?
        )
2015-06-03 21:41:18,149  TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - 58
2015-06-03 21:41:18,149  TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BOOLEAN] - true
2015-06-03 21:41:18,149  TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - 26
2015-06-03 21:41:18,149  WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 22023
2015-06-03 21:41:18,149  ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - The column index is out of range: 3, number of columns: 2.

暂时的解决方法

需要继续做一些其他的事情...由于删除是在尝试删除之前在内部执行选择,这不会比我试图实现的原始代码更损害性能

List<MatchPronostic> pronosticsToDelete = matchPronosticsRepository.findAll(ids);
matchPronosticsRepository.deleteInBatch(pronosticsToDelete);

【问题讨论】:

    标签: java hibernate jpa repository


    【解决方案1】:

    不是@Query,而是像here 中描述的那样为您的存储库提供自定义方法如何?

    所以基本上,我的建议是这样的:

    public void myCustomDel(Iterable<MyID> ids) {
      delete(findAll(ids));
    }
    

    【讨论】:

    • 是的,这基本上就是我发布的“解决方法”。您的选项可以使用新的 J8 默认方法实现,因此可以保留在 JPA 接口中。但我认为会有一个基于列表删除的选项,而无需先去获取实体
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2010-12-23
    • 2018-07-17
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多