Hibernate Table replication with Quence

Asked

Viewed 21 times

1

Good Afternoon, In an application I am working on we have a table called tb_perfilparametrovalorpdv, following the mapping

@Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "tb_perfilparametrovalorpdv")
@SequenceGenerator(name = "gen_perfilparametrovalorpdv", sequenceName = "gen_perfilparametrovalorpdv", allocationSize = 1)
public class PerfilParametroValorPDV implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id_perfilparametrovalorpdv", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "gen_perfilparametrovalorpdv")
private Long id;

@Column(name = "tx_descricao", nullable = false)
private String descricao;

@Column(name = "dt_cadastro", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreationTimestamp
private Date dataCadastro;

@Column(name = "dt_manutencao", nullable = true)
@Temporal(TemporalType.TIMESTAMP)
@UpdateTimestamp
private Date dataManutencao;

@Column(name = "nr_sincronizacaoversao", nullable = true)
private Long sincronizacaoVersao;

@Column(name = "dt_exclusao", nullable = true)
@Temporal(TemporalType.TIMESTAMP)
private Date dataExclusao;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "parametroValorPDV", fetch = FetchType.LAZY)
private List<ParametroValorPDV> parametroValorPDVs = new ArrayList<>();

@OneToMany(mappedBy = "parametroValorPDV", fetch = FetchType.LAZY)
private List<VinculoPerfilPDV> vinculoPerfilPDV = new ArrayList<>();

}

We have two applications running in separate databases, where we call one Multi and the other Terminal. Since in Multi we create the records and use Quence, and in the terminal we only synchronize the records. We pass the information from the Multi to the Terminal through a REST API, including passing the id generated by the Multi Quence, but when trying to make the persistence in the terminal by the code

   @Override
@Transactional(rollbackFor = Exception.class)
public void sincronizarCarga(List<PerfilParametroValorPDVDTO> listDadosRemoto) throws Exception {

    if (!ListUtil.isNullOrEmpty(listDadosRemoto)) {

        List<PerfilParametroValorPDV> perfis = modelMapper.map(listDadosRemoto,
                TypeToken.getParameterized(List.class, PerfilParametroValorPDV.class).getType());

        perfis.forEach(perfilParametroValorPDVRepository::save);
    }
}

Hibernate ignores the id passed by DTO and tries to generate a new one. Is there any way to make Hibernate ignore Sequence when receiving an id?

1 answer

1

Staff managed to solve by creating a custom Enerator.

Follows codes:

package br.com.sysmo.server.pdv.repository.generators;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;

public class S1ServerPDVCustomGenerator extends SequenceStyleGenerator {

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {

    Serializable id = session.getEntityPersister(object.getClass().getName(), object).getIdentifier(object, session);

    if (id != null) {
        return id;
    }

    return super.generate(session, object);
}

}

and on the model:

@GenericGenerator(name = "gen_perfilparametrovalorpdv", strategy = "br.com.sysmo.server.pdv.repository.generators.S1ServerPDVCustomGenerator", parameters = {
    @Parameter(name = "sequence_name", value = "gen_perfilparametrovalorpdv"), @Parameter(name = "initial_value", value = "1"),
    @Parameter(name = "increment_size", value = "1") })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.