1
Guys, I’m running a data migration from a csv file to an H2 database. In this process I use the beanio to read the file and transform it into objects and then persist the objects in the base using jpa+Hibernate.
The problem is weird because when reading the file the description property of the first object read is
Does anyone have any idea what this is?
Beanio xml
<stream name="produto" format="csv">
<parser>
<property name="delimiter" value=";" />
<property name="lineSeparator" value="\n" />
<property name="whitespaceAllowed" value="true" />
</parser>
<typeHandler type="java.util.Date" class="com.nexccs.typehandlers.DateTypeHandler" />
<typeHandler name="LongHandler" class="com.nexccs.typehandlers.LongTypeHandler" />
<typeHandler name="IntegerHandler" class="com.nexccs.typehandlers.IntegerTypeHandler" />
<typeHandler name="UsuarioHandler" class="com.nexccs.typehandlers.UsuarioTypeHandler" />
<typeHandler name="CategoriaHandler" class="com.nexccs.typehandlers.StringCategoriaHandler" />
<typeHandler name="UnidadeMedidaHandler" class="com.nexccs.typehandlers.StringUnidadeMedidaHandler" />
<typeHandler name="FornecedorHandler" class="com.nexccs.typehandlers.FornecedorHandler" />
<typeHandler name="MoneyHandler" class="com.nexccs.typehandlers.MoneyToBigDecimalHandler" />
<typeHandler name="CommaToDecimalPointerBigDecimal" class="com.nexccs.typehandlers.CommaToDecimalPointerBigDecimalTypeHandler" />
<record name="produto" class="com.nexccs.model.Produto">
<field name="descricao" />
<field name="codigo" />
<field name="qtdEstoque" typeHandler="CommaToDecimalPointerBigDecimal"/>
<field name="preco" typeHandler="MoneyHandler"/>
<field name="margem" typeHandler="CommaToDecimalPointerBigDecimal"/>
<field name="custo" typeHandler="MoneyHandler"/>
<segment name="categoria" class="com.nexccs.model.Categoria" lazy="true">
<field name="descricao" typeHandler="CategoriaHandler"/>
</segment>
<segment name="unidadeMedida" class="com.nexccs.model.UnidadeMedida" lazy="true">
<field name="descricao" typeHandler="UnidadeMedidaHandler"/>
</segment>
<segment name="fornecedor" class="com.nexccs.model.Fornecedor" lazy="true">
<field name="id" typeHandler="FornecedorHandler" trim="true"/>
</segment>
<field name="tributacao" />
<field name="ncm" />
</record>
</stream>
Product class
@Entity @Table(name="produtos")
public class Produto implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="cod_produto")
private String codigo;
@Column(name="desc_produto")
private String descricao;
Excerpt from the code that parses and saves to db
StreamFactory factory = StreamFactory.newInstance();
// load the mapping file
factory.load(resourcePath_beanio + tabelaArquivo[0].toLowerCase() + ".xml");
//create reader
BeanReader in = factory.createReader(tabelaArquivo[0].toLowerCase(), new File(tabelaArquivo[1]));
Object entity;
//list of details
Set<Object> entities = new HashSet<>();
//add details to list
while ((entity = (Object) in.read()) != null) {
entities.add(entity);
}
Class<AbstractDAO<Object, Long>> dao = (Class<AbstractDAO<Object, Long>>) Class.forName(packageRoot_dao + tabelaArquivo[0] + "DAO");
Ambiente.getInstance().setBatch();
//save details on DB
dao.newInstance().saveAll(entities);
...
Post your object with annotations and xml;
– alxwca
The worst is that only the first csv file record happens this, the other records are normal.
– penunes
he’s picking up the header, put your controller, where you do the Beanreader Instance
– alxwca
It was the header... I put a Skip at the time of reading and added a blank line in the files... I found it very strange the beanio not recognize only the details
– penunes