Consulta gera um update com JPA

4 respostas
M

Pessoal boa tarde,

Estou com um problema na minha aplicação onde após a aplicação fazer um select na base, ela gera um update na tabela. Fui debugando de verifiquei que esse update é gerado quando ele entra na classe txInterceptor.class. A aplicação em um ear e estou usando EJB3 e JPA 1.0. Alguém sabe me dizer se está correto isso ou se tem algum erro no processo ou no mapeamento ou até alguma anotação que não tenha colocado e que possa resolver meu problema?

4 Respostas

A

cara,

posta suas entidades e o codigo aonde está ocorrendo isso?

t+

M

Minha entidade…

package br.com.sialc.ejb.entity;

// Generated 11/11/2011 17:50:27 by Hibernate Tools 3.4.0.CR1

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import br.com.sialc.tools.util.Formatter;
import br.com.sialc.tools.util.ObjectUtils;

/**
 * LimiteCredito generated by hbm2java
 */
@Entity
@Table(name = "alctb016_limite_credito")
@SequenceGenerator(name = "seq_LimiteCredito", sequenceName = "seq_co_limite_credito")
public class LimiteCredito implements Serializable {
	private static final long serialVersionUID = -3846674204144728175L;
	private Long codigoLimiteCredito;
	private Produto produto;
	private String numeroAgencia;
	private String cpfCnpj;
	private BigDecimal valorAutorizado;
	private String numeroResolucao;
	private Date dataResolucao;
	private Long prazoAutorizacao;
	private String observacao;
	private Set<DesembolsoReembolso> desembolsosReembolsos = new HashSet<DesembolsoReembolso>(
			0);

	@Id
	@Column(name = "co_limite_credito", unique = true, nullable = false)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_LimiteCredito")
	public Long getCodigoLimiteCredito() {
		return this.codigoLimiteCredito;
	}

	public void setCodigoLimiteCredito(final Long coLimiteCredito) {
		this.codigoLimiteCredito = coLimiteCredito;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "co_produto", nullable = false)
	public Produto getProduto() {
		return this.produto;
	}

	public void setProduto(final Produto produto) {
		this.produto = produto;
	}

	@Column(name = "nu_agencia", nullable = false, length = 4)
	public String getNumeroAgencia() {
		return this.numeroAgencia;
	}

	public void setNumeroAgencia(final String nuAgencia) {
		this.numeroAgencia = nuAgencia;
	}

	@Column(name = "co_cpf_cnpj", nullable = false, length = 14)
	public String getCpfCnpj() {
	    if (ObjectUtils.isNullOrEmpty(this.cpfCnpj)) {
	        return "";
	    }
		return this.cpfCnpj.trim();
	}

	public void setCpfCnpj(final String coCpfCnpj) {
		this.cpfCnpj = coCpfCnpj;
	}

	@Column(name = "vr_autorizado", nullable = false, precision = 20)
	public BigDecimal getValorAutorizado() {
		return this.valorAutorizado;
	}

	public void setValorAutorizado(final BigDecimal vrAutorizado) {
		this.valorAutorizado = vrAutorizado;
	}

	@Column(name = "nu_resolucao", nullable = false, length = 6)
	public String getNumeroResolucao() {
	    if (ObjectUtils.isNullOrEmpty(this.numeroResolucao)) {
	        return "";
	    }
		return this.numeroResolucao.trim();
	}

	public void setNumeroResolucao(final String nuResolucao) {
		this.numeroResolucao = nuResolucao;
	}

	@Column(name = "dt_resolucao", nullable = false, length = 13)
	public Date getDataResolucao() {
		return this.dataResolucao;
	}

	public void setDataResolucao(final Date dtResolucao) {
		this.dataResolucao = dtResolucao;
	}

	@Column(name = "pz_autorizacao", nullable = false)
	public Long getPrazoAutorizacao() {
		return this.prazoAutorizacao;
	}

	public void setPrazoAutorizacao(final Long pzAutorizacao) {
		this.prazoAutorizacao = pzAutorizacao;
	}

	@Column(name = "de_observacao")
	public String getObservacao() {
		return this.observacao;
	}

	public void setObservacao(final String deObservacao) {
		this.observacao = deObservacao;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "limiteCredito")
	public Set<DesembolsoReembolso> getDesembolsosReembolsos() {
		return this.desembolsosReembolsos;
	}

	public void setDesembolsosReembolsos(
			final Set<DesembolsoReembolso> desembolsoReembolsos) {
		this.desembolsosReembolsos = desembolsoReembolsos;
	}

    /**
     * Obtendo o valor do atributo.
     * 
     * @return String
     */
    @Transient
    public String getCnpjFormatado() {
        if (ObjectUtils.isNullOrEmpty(this.getCpfCnpj())) {
            return "";
        }
        return Formatter.formatCpfCnpj(this.getCpfCnpj());
    }
    
    @Transient
    public String getOperacaoFormatada() {
        if (ObjectUtils.isNullOrEmpty(this.getProduto(),
                this.getProduto().getCodigoProduto(),
                this.getProduto().getNomeProduto())) {
            return "";
        }
        return this.getProduto().getCodigoProduto().toString() + " - " +
            this.getProduto().getNomeProduto();
    }

//	public String getStringData(){
//		return DateUtils.toString(this.getDataResolucao(), DateFormat.DATE);
//	}
	

}

e o problema ocorre no retorno desse metodo:

@Override
    public List<T> limitedOrderedList(final int start, final int limit,
            final String field, final OrderType type, final String filter,
            final String[] fields, final EntityManager entityManager) {
        this.debug(this.getMessage().getMessage("bean.limited.ordered.list",
                new String[] {
                        String.valueOf(start), String.valueOf(limit), field,
                        type.getType(), this.getGenericClass().getName(),
                }));
        try {
            final String hql = "FROM " + this.getGenericClass().getName()
                    + this.generateWhereValue(filter, fields) + " ORDER BY "
                    + field + " " + type.getType();
            final List<T> result = (List<T>) this.createQuery(start, limit, 
                    entityManager, hql).getResultList();
            this.debug(this.getMessage()
                    .getMessage("bean.limited.ordered.list.success"));
            return result;
        } catch (NoResultException exception) {
            return new ArrayList<T>();
        } catch (RuntimeException exception) {
            this.error(
                    this.getMessage().getMessage("bean.limited.ordered.list.fail"),
                    exception);
            return new ArrayList<T>();
        }
    }

Mas o update acontece dentro do TxInterceptor.class

A

estranho cara,

unica coisa que achei, nao sei se é isso tbm, será q não é pelo EAGER no mapeamento de produto.

t+

M

Cara acredito que não seja esse o problema.
Mas vou testar e te respondo.

Criado 30 de novembro de 2011
Ultima resposta 30 de nov. de 2011
Respostas 4
Participantes 2