带有自定义FieldSetMapper的Spring Batch是否不保存数据?

十:

我需要将一个大的.csv加载到我的数据库中,我有一个Price实体,该实体引用了一个Distributor:

public class Price {

@Id
private String id;

private Date dtComu;

private Double price;

@ManyToOne
@JoinColumn(name = "idDistributor")
private Distributor distributor;

//constrcutor, getters&setters

我尝试上传的带有价格数据的csv对每个价格都有对分销商ID的引用。在ItemReader上,我有:

public FlatFileItemReader<Price> priceReader() {

    FlatFileItemReader<Price> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("file.csv"));
    reader.setLinesToSkip(2);
    reader.setRecordSeparatorPolicy(recordSeparatorPolicy);
    reader.setLineMapper(new DefaultLineMapper<Price>() {
        {
            setLineTokenizer(new DelimitedLineTokenizer() {
                {
                    setStrict(false);
                    setDelimiter(";");
                    setNames(new String[] { "idDistributor", "price", "dtComu" });
                }
            });
            setFieldSetMapper(customMapper());

        }
    });

    return reader;
}

我的customMapper是:

public class CustomMapper implements FieldSetMapper<Price> {

@Autowired
DistributorRepository repository;

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

@Override
public Price mapFieldSet(FieldSet fieldSet) throws BindException {
    Distributor distributor = repository.findById(fieldSet.readInt("idDistributor")).orElse(null);
    if (distributore == null) {
        return null;
    }
    Price p = new Price();
    p.setDistributor(distributor);
    p.setPrice(fieldSet.readDouble("price"));
    try {
        p.setDtComu(formatter.parse(fieldSet.readString("dtComu")));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        p.setDtComu(new Date());
    }
    //here i will crate an ID for the Price that i need to always be unique
    p.setId(distributor.getIdImpianto()+ fieldSet.readString("dtComu"));
    return p;   }}

我的作家如下:

public JdbcBatchItemWriter<Price> prezzoWriter() {
    JdbcBatchItemWriter<price> writer = new JdbcBatchItemWriter<Price>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Prezzo>());
    ;
    writer.setSql("INSERT INTO price (id_distributor, price, dt_comu) "
            + "VALUES (:idDistributor, :price, :dtComu");
    writer.setDataSource(dataSource);
    return writer;
}

我运行该程序,它没有任何保存就完成了。我会做错什么吗?我只是从Sring Batch开始的,所以必须详细说明一下。

Mahmoud Ben Hassine:

JdbcBatchItemWriter一无所知的情况下JPA。您需要使用a DataSourceTransactionManager才能正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Set <String>是否不保存所有数据?

保存自定义帖子元,不保存数据

WordPress 自定义元框不保存数据或显示

Django:带有queryset的自定义保存方法

libcurl:带有自定义数据的POST请求

提供带有自定义参数的数据的设计模式

存储带有核心数据的自定义类

如何使用带有所有自定义属性的ggsave保存自定义图?

带有自定义数据库的AWS AppSync是否支持订阅?

上传的带有 DRF 数据的模型数据不保存

Java不保存所有数据

Sharepoint 2013/2016和带有自定义数据库的自定义Web Api。

是否可以定义带有环回选项的自定义验证?

Spring Batch:自定义ItemReader

Spring Batch复杂的自定义ItemReader

Android Fragments-在哪里保存状态(带有自定义对象的链表)?

在 Android 中使用 GSON 添加带有 API 数据的自定义 ArrayList 数据

Android XML是否可以使用带有自定义属性的工具名称空间?

从Label派生的WPF控件:自定义属性(带有DP)是否未使用setter?

如何检查带有自定义类的向量是否为NULL?

带有网格的“自定义”选项卡未在导航主数据时刷新

带有 Firebase 数据库的 UITableView 中的多个自定义单元格

如何使用 where 子句从带有自定义字段的数据库中获取记录?

带有自定义用户数据的 Angular Firebase Google 身份验证

在WooCommerce中添加带有价格不含税的自定义订单元数据

Keras自定义损失函数,带有来自完整输入数据集的样本

带有客户端验证的自定义数据注释验证属性

将带有自定义标签的数据透视表作为行

带有数据注释的枚举类型的Json.NET自定义序列化