JPARepository deleteAllInBatch()无法按预期运行

时间:2019-06-15 22:03:54

标签: spring spring-data-jpa spring-data

我试图使用Spring JPA deleteAllInBatch()从我的表中删除一些行,但是当要删除的行数超过某个阈值时,JPA会引发错误。我不确定导致此错误的原因,但发现了一张吉拉票:https://jira.spring.io/browse/DATAJPA-137

我不想使用deleteAll(),因为它会一一删除数据,这会导致性能问题。这是JPA的缺点还是有一些解决方案。我尝试了一些解决方法,但没有发现任何有用的方法。请帮助我获得此操作的有效解决方案或一些有用的参考。预先感谢...

DbIssueApplication.java

@SpringBootApplication
public class DbIssueApplication 
{
    public static void main(String[] args) 
    {
        ApplicationContext context = SpringApplication.run(DbIssueApplication.class, args);
        TestService service = context.getBean(TestService.class);

        long st = System.currentTimeMillis();

        List<Test> testList = new ArrayList<>();        
        for(int i=0;i<5000;i++)
        {
            testList.add(new Test(i,(i%2==0)?"field1":"field2"));
        }
        service.insert(testList);


        service.deleteByName("field2");

        System.err.println("The processing took = "+(System.currentTimeMillis()-st)+" ms");
    }

}

Test.java

@Entity
@Table(name="test")
public class Test implements Serializable 
{
    private static final long serialVersionUID = -9182756617906316269L;
    @Id
    private Integer id; 
    private String name;

        ... getter,setter and constructors

}

TestRepository.java

public interface TestRepository extends JpaRepository<Test, Integer> 
{
    List<Test> findByName(String name);
}

TestService.java

public interface TestService 
{
    public void insert(List<Test> testList);
    public void deleteByName(String name);
}

TestServiceImpl.java

@Service
public class TestServiceImpl implements TestService 
{
    @Autowired
    TestRepository testRepository;

    @Override
    public void insert(List<Test> testList) 
    {
        testRepository.deleteAllInBatch();
        testRepository.saveAll(testList);
    }

    @Override
    public void deleteByName(String name)
    {
        System.err.println("The number of rows to be deleted = "+testRepository.findByName(name).size());
        testRepository.deleteInBatch(testRepository.findByName(name));
    }


}

dbSchema

create table test
(
    id int,
    name varchar(40)
);

ErrorLog

[           main] o.h.e.t.i.TransactionImpl                : begin
[           main] o.h.h.i.a.QueryTranslatorImpl            : parse() - HQL: delete from com.example.demo.entity.Test x where x = ?1 or x = ?2 or x = ?3 or x = ?4 or ...  x = ?2500
[           main] o.h.h.i.a.ErrorTracker                   : throwQueryException() : no errors
[           main] o.h.e.t.i.TransactionImpl                : rolling back
[      Thread-14] o.h.i.SessionFactoryImpl                 : HHH000031: Closing
[      Thread-14] o.h.t.s.TypeConfiguration$Scope          : Un-scoping TypeConfiguration [org.hibernate.type.spi.TypeConfiguration$Scope@6cf001] from SessionFactory [org.hibernate.internal.SessionFactoryImpl@1ad3d8a]
[      Thread-14] o.h.s.i.AbstractServiceRegistryImpl      : Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
[      Thread-14] o.h.b.r.i.BootstrapServiceRegistryImpl   : Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries

=======================================================================================================================================================================================================================================================================================================

[           main] o.h.h.i.QueryTranslatorFactoryInitiator  : QueryTranslatorFactory : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory@5e167a
[           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
[           main] o.h.h.i.a.QueryTranslatorImpl            : parse() - HQL: select generatedAlias0 from com.example.demo.entity.Test as generatedAlias0 where generatedAlias0.name=:param0
[           main] o.h.h.i.a.ErrorTracker                   : throwQueryException() : no errors
[           main] o.h.h.i.a.QueryTranslatorImpl            : --- HQL AST ---
 \-[QUERY] Node: 'query'
    +-[SELECT_FROM] Node: 'SELECT_FROM'
    |  +-[FROM] Node: 'from'
    |  |  \-[RANGE] Node: 'RANGE'
    |  |     +-[DOT] Node: '.'
    |  |     |  +-[DOT] Node: '.'
    |  |     |  |  +-[DOT] Node: '.'
    |  |     |  |  |  +-[DOT] Node: '.'
    |  |     |  |  |  |  +-[IDENT] Node: 'com'
    |  |     |  |  |  |  \-[IDENT] Node: 'example'
    |  |     |  |  |  \-[IDENT] Node: 'demo'
    |  |     |  |  \-[IDENT] Node: 'entity'
    |  |     |  \-[IDENT] Node: 'Test'
    |  |     \-[ALIAS] Node: 'generatedAlias0'
    |  \-[SELECT] Node: 'select'
    |     \-[IDENT] Node: 'generatedAlias0'
    \-[WHERE] Node: 'where'
       \-[EQ] Node: '='
          +-[DOT] Node: '.'
          |  +-[IDENT] Node: 'generatedAlias0'
          |  \-[IDENT] Node: 'name'
          \-[COLON] Node: ':'
             \-[IDENT] Node: 'param0'

[           main] o.h.h.i.a.HqlSqlBaseWalker               : select << begin [level=1, statement=select]
[           main] o.h.h.i.a.t.FromElement                  : FromClause{level=1} : com.example.demo.entity.Test (generatedAlias0) -> test0_
[           main] o.h.h.i.a.t.FromReferenceNode            : Resolved : generatedAlias0 -> test0_.id
[           main] o.h.h.i.a.t.FromReferenceNode            : Resolved : generatedAlias0 -> test0_.id
[           main] o.h.h.i.a.t.DotNode                      : getDataType() : name -> org.hibernate.type.StringType@d003cd
[           main] o.h.h.i.a.t.FromReferenceNode            : Resolved : generatedAlias0.name -> test0_.name
[           main] o.h.h.i.a.HqlSqlBaseWalker               : select : finishing up [level=1, statement=select]
[           main] o.h.h.i.a.HqlSqlWalker                   : processQuery() :  ( SELECT ( {select clause} test0_.id ) ( FromClause{level=1} test test0_ ) ( where ( = ( test0_.name test0_.id name ) ? ) ) )
[           main] o.h.h.i.a.u.JoinProcessor                : Using FROM fragment [test test0_]
[           main] o.h.h.i.a.HqlSqlBaseWalker               : select >> end [level=1, statement=select]
[           main] o.h.h.i.a.QueryTranslatorImpl            : --- SQL AST ---
 \-[SELECT] QueryNode: 'SELECT'  querySpaces (test)
    +-[SELECT_CLAUSE] SelectClause: '{select clause}'
    |  +-[ALIAS_REF] IdentNode: 'test0_.id as id1_0_' {alias=generatedAlias0, className=com.example.demo.entity.Test, tableAlias=test0_}
    |  \-[SQL_TOKEN] SqlFragment: 'test0_.name as name2_0_'
    +-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[generatedAlias0], fromElementByTableAlias=[test0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
    |  \-[FROM_FRAGMENT] FromElement: 'test test0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=generatedAlias0,role=null,tableName=test,tableAlias=test0_,origin=null,columns={,className=com.example.demo.entity.Test}}
    \-[WHERE] SqlNode: 'where'
       \-[EQ] BinaryLogicOperatorNode: '='
          +-[DOT] DotNode: 'test0_.name' {propertyName=name,dereferenceType=PRIMITIVE,getPropertyPath=name,path=generatedAlias0.name,tableAlias=test0_,className=com.example.demo.entity.Test,classAlias=generatedAlias0}
          |  +-[ALIAS_REF] IdentNode: 'test0_.id' {alias=generatedAlias0, className=com.example.demo.entity.Test, tableAlias=test0_}
          |  \-[IDENT] IdentNode: 'name' {originalText=name}
          \-[NAMED_PARAM] ParameterNode: '?' {name=param0, expectedType=org.hibernate.type.StringType@d003cd}

[           main] o.h.h.i.a.ErrorTracker                   : throwQueryException() : no errors
[           main] o.h.h.i.a.QueryTranslatorImpl            : HQL: select generatedAlias0 from com.example.demo.entity.Test as generatedAlias0 where generatedAlias0.name=:param0
[           main] o.h.h.i.a.QueryTranslatorImpl            : SQL: select test0_.id as id1_0_, test0_.name as name2_0_ from test test0_ where test0_.name=?
[           main] o.h.h.i.a.ErrorTracker                   : throwQueryException() : no errors
[           main] o.h.h.i.a.QueryTranslatorImpl            : parse() - HQL: delete from com.example.demo.entity.Test x where x = ?1 or x = ?2 ... or x = ?2500
[           main] o.h.h.i.a.ErrorTracker                   : throwQueryException() : no errors


示例代码在github中上传,其链接为:https://github.com/Anand450623/Stackoverflow

1 个答案:

答案 0 :(得分:0)

您可以尝试使用JPQL查询来批量deleteAll()而不是一个一个地删除。

但是,您实际上可能实际上希望完全删除orm-framework。常见的经验是,尽管一开始它似乎是个好主意,但几乎会遇到您在此处提出的问题:/您可以阅读https://www.toptal.com/java/how-hibernate-ruined-my-career的要旨是:很难调试,无论如何,您都无法避免在大多数情况下编写本机SQL,JPQL限制了您的表达能力,并且建模方式极具侵入性(例如,在很多情况下您都无法做到不变)。

Spring有一个出色的JdbcTemplate,但是请记住,它也有缺点,主要是您必须自己编写映射-表示代码并不多。那就是说好处是巨大的。因此,如果JPQL查询不起作用,请考虑使用JPA(休眠吗?)是否是正确的开始选择