使用Hibernate更新数据库表的更快方法(Java 8减少?)

时间:2017-05-01 14:14:49

标签: java mysql spring hibernate java-8

我正在开发一个使用 Hibernate 作为ORM在 Spring Boot 中开发的监控工具。

我需要比较表格中的每一行(已保留的已发送邮件行),看看MailId(唯一)是否收到了反馈(状态:OPENEDBOUNCEDDELIVERED ...)是或否。

我通过从网络文件夹中读取csv文件来获得反馈。 CSV解析和文件读取速度非常快,但我的数据库更新速度非常慢。我的算法效率不高,因为我通过一个可以拥有数十万个对象的列表循环并查看我的表格。

这是通过更新" target"在我的表中进行更新的方法。对象(表数据库中的行)

@Override
public void updateTargetObjectFoo() throws CSVProcessingException, FileNotFoundException {             
    // Here I make a call to performProcessing method which reads files on a folder and parse them to JavaObjects and I map them in a feedBackList of type Foo
    List<Foo> feedBackList = performProcessing(env.getProperty("foo_in"), EXPECTED_HEADER_FIELDS_STATUS, Foo.class, ".LETTERS.STATUS.");
    for (Foo foo: feedBackList) {                        
        //findByKey does a simple Select in mySql where MailId = foo.getMailId()
        Foo persistedFoo = fooDao.findByKey(foo.getMailId());
        if (persistedFoo != null) {
            persistedFoo.setStatus(foo.getStatus());
            persistedFoo.setDnsCode(foo.getDnsCode());             
            persistedFoo.setReturnDate(foo.getReturnDate());
            persistedFoo.setReturnTime(foo.getReturnTime());   
            //The save account here does an MySql UPDATE on the table
            fooDao.saveAccount(foo);
        }
    }
}

如果我在Java端实现此选择/比较和更新操作怎么办?然后重新更新数据库中的整个列表?

会更快吗?

感谢大家的帮助。

1 个答案:

答案 0 :(得分:2)

Hibernate不是特别适合批量处理。 使用Spring的JdbcTemplate进行jdbc批处理可能会更好。

但是,如果您必须通过Hibernate执行此操作,这可能有所帮助:https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/batch/Batching.html