Java 使用JBoss Seam中的EntityHome合并JPA实体

Java 使用JBoss Seam中的EntityHome合并JPA实体,java,hibernate,jpa,seam2,Java,Hibernate,Jpa,Seam2,我试图使用JBoss Seam和JPA更新实体,但我面临着分离实体的常见问题。我会解释得更好 我有两个简单的实体是相关的。这种关系通过注释映射。看一看: @Entity @Table(name = "RATEIO", uniqueConstraints = { @UniqueConstraint(columnNames = { "cd_projeto", "cd_tarefa", "cd_colaborador", "dt_rateio_inicial", "dt_rateio_

我试图使用JBoss Seam和JPA更新实体,但我面临着分离实体的常见问题。我会解释得更好

我有两个简单的实体是相关的。这种关系通过注释映射。看一看:

@Entity
@Table(name = "RATEIO", uniqueConstraints = { @UniqueConstraint(columnNames = { "cd_projeto", "cd_tarefa", "cd_colaborador", "dt_rateio_inicial",
        "dt_rateio_final" }) })
@Audited
public class Rateio implements java.io.Serializable {

    private static final long serialVersionUID = 1564844894403478898L;

    private Long cdRateio;
    private Colaborador colaborador;
    private Tarefa tarefa;
    private Projeto projeto;
    private Date dtRateioInicial;
    private Date dtRateioFinal;

    private boolean flagAtividade;
    private TipoRateio tipo;
    private List<DetalheRateio> detalheRateios = new ArrayList<DetalheRateio>(0);

    public Rateio() {
    }

    public Rateio(Long cdRateio, Colaborador colaborador, Projeto projeto, Date dtRateioInicial, Date dtRateioFinal) {
        this.cdRateio = cdRateio;
        this.colaborador = colaborador;
        this.projeto = projeto;
        this.dtRateioInicial = dtRateioInicial;
        this.dtRateioFinal = dtRateioFinal;
    }

    public Rateio(Long cdRateio, Colaborador colaborador, Projeto projeto, Date dtRateioInicial, Date dtRateioFinal, List<DetalheRateio> detalheRateios) {
        this.cdRateio = cdRateio;
        this.colaborador = colaborador;
        this.projeto = projeto;
        this.dtRateioInicial = dtRateioInicial;
        this.dtRateioFinal = dtRateioFinal;
        this.detalheRateios = detalheRateios;
    }

    @Id
    @SequenceGenerator(name = "sg_RATEIO_SEQ", sequenceName = "RATEIO_SEQ", allocationSize = 1)
    @GeneratedValue(generator = "sg_RATEIO_SEQ")
    @Column(name = "CD_RATEIO", nullable = false, scale = 0)
    public Long getCdRateio() {
        return this.cdRateio;
    }

    public void setCdRateio(Long cdRateio) {
        this.cdRateio = cdRateio;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CD_COLABORADOR", nullable = true)
    public Colaborador getColaborador() {
        return this.colaborador;
    }

    public void setColaborador(Colaborador colaborador) {
        this.colaborador = colaborador;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CD_PROJETO", nullable = true)
    public Projeto getProjeto() {
        return this.projeto;
    }

    public void setProjeto(Projeto projeto) {
        this.projeto = projeto;
    }

    @Column(name = "DT_RATEIO_INICIAL", nullable = false)
    public Date getDtRateioInicial() {
        return this.dtRateioInicial;
    }

    public void setDtRateioInicial(Date dtRateioInicial) {
        this.dtRateioInicial = dtRateioInicial;
    }

    @Column(name = "DT_RATEIO_FINAL", nullable = true)
    public Date getDtRateioFinal() {
        return this.dtRateioFinal;
    }

    public void setDtRateioFinal(Date dtRateioFinal) {
        this.dtRateioFinal = dtRateioFinal;
    }

    @OneToMany(targetEntity=DetalheRateio.class, mappedBy = "rateio")
    public List<DetalheRateio> getDetalheRateios() {
        return this.detalheRateios;
    }

    public void setDetalheRateios(List<DetalheRateio> detalheRateios) {
        this.detalheRateios = detalheRateios;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CD_TAREFA", nullable = true)
    public Tarefa getTarefa() {
        return tarefa;
    }

    public void setTarefa(Tarefa tarefa) {
        this.tarefa = tarefa;
    }

    @Enumerated(EnumType.ORDINAL)
    public TipoRateio getTipo() {
        return tipo;
    }

    public void setTipo(TipoRateio tipo) {
        this.tipo = tipo;
    }

    @Transient
    public boolean isFlagAtividade() {
        return flagAtividade;
    }

    public void setFlagAtividade(boolean flagAtividade) {
        this.flagAtividade = flagAtividade;
    }
}
好的,到目前为止,一切都很好。现在,我有一个从JBossSeam扩展而来的类,这个类试图更新我的实体。代码如下:

@Name("rateioHome")
public class RateioHome extends EntityHome<Rateio> {

    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public String update() {
        String result = null;
        boolean valid = true;
        if (getPercentualTotal().doubleValue() < 100 || instance.getDetalheRateios().size() < 2) {
            facesMessages.addFromResourceBundle(Severity.ERROR, "rateio.erro.atividade");
            valid = false;
        }

        if (instance.getDtRateioFinal() != null && instance.getDtRateioFinal().before(instance.getDtRateioInicial())) {
            facesMessages.addFromResourceBundle(Severity.ERROR, "rateio.erro.data");
            valid = false;
        }

        if (valid) {
            Rateio rateio = super.find();
            List<DetalheRateio> detalheRateios = getInstance().getDetalheRateios();
            List<DetalheRateio> detalheRateiosBd = rateio.getDetalheRateios();
            Map<Long, DetalheRateio> mapDetalheRateio = new HashMap<Long, DetalheRateio>();

            if (detalheRateiosBd != null && detalheRateiosBd.size() > 0) {

                Rateio _rateio = getInstance();

                for (DetalheRateio item : detalheRateiosBd) {

                    if (item.getCdDetalheRateio() != null) {

                        item.getRateio().setDtRateioInicial(_rateio.getDtRateioInicial());
                        item.getRateio().setDtRateioFinal(_rateio.getDtRateioFinal());
                        mapDetalheRateio.put(item.getCdDetalheRateio(), item);
                    }
                }
            }

            List<DetalheRateio> novos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateios, detalheRateiosBd);
            List<DetalheRateio> excluidos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateiosBd, detalheRateios);

            detalheRateiosBd.removeAll(excluidos);
            detalheRateiosBd.addAll(novos);

            setInstance(rateio);
            result = super.update();
            getEntityManager().flush();
        }
        return result;
    }
}
现在,问题来了。当我更新第行的实体时:result=super.update;,我面临着一个例外:

javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:传递给persistent的分离实体:br.com.entity.DetalheRateio


我试图在EntityHome上找到merge方法,但没有找到任何结果。这是我必须支持的遗留代码,我不是JBossSeam方面的专家。如果您需要更多信息,请告诉我。

刚刚找到解决方案。更新对象速率之前,必须合并并保持对象DetalheRateio列表。我认为对象DetalheRateio作为Rateio子对象将由JPA自动持久化。我的错误。因此,我手动合并了每个DetalheRateio对象。我以前做这件事的鹬:

List<DetalheRateio> novos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateios, detalheRateiosBd);
List<DetalheRateio> excluidos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateiosBd, detalheRateios);
List<DetalheRateio> detalhesAlterados = new ArrayList<DetalheRateio>();

detalheRateiosBd.removeAll(excluidos);
detalheRateiosBd.addAll(novos);

for (DetalheRateio detalheExcluido : excluidos) {
    getEntityManager().remove(detalheExcluido);
}

for (DetalheRateio dRateio : detalheRateios) {
    detalhesAlterados.add(getEntityManager().merge(dRateio));
}

rateio.setDetalheRateios(detalhesAlterados);
setInstance(rateio);
result = super.update();
getEntityManager().flush();

合并不会有帮助,因为您的比率实体实际上是受管理的。错误是抱怨一个孩子DetalheRateio被分离了。也许您正在某处添加一个新实例,而没有先将其持久化?您好。我想我正在分离这里的子对象:List detalheRateiosBd=rateio.getDetalheRateios;。这个对象是更新的值,我将它设置回Rateio对象。现在,我如何更新EntityHome中的子对象JPA?
List<DetalheRateio> novos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateios, detalheRateiosBd);
List<DetalheRateio> excluidos = (List<DetalheRateio>) CollectionUtils.subtract(detalheRateiosBd, detalheRateios);
List<DetalheRateio> detalhesAlterados = new ArrayList<DetalheRateio>();

detalheRateiosBd.removeAll(excluidos);
detalheRateiosBd.addAll(novos);

for (DetalheRateio detalheExcluido : excluidos) {
    getEntityManager().remove(detalheExcluido);
}

for (DetalheRateio dRateio : detalheRateios) {
    detalhesAlterados.add(getEntityManager().merge(dRateio));
}

rateio.setDetalheRateios(detalhesAlterados);
setInstance(rateio);
result = super.update();
getEntityManager().flush();