JPA - Problema com PERSIST

0 respostas
UpTheIrons

Olá pessoal,

Fazendo uns testes de performance com JPA, criei um método pra inserção de registros em cascata
e funciona bem, gostei da performance e tal.
Só q notei um prob, qdo passo 100 registros como parâmetro, q na verdade persiste 100 pessoas e 500 números (5 para cada pessoa),
qdo faço isso repetidamente, vez ou outra dá um prob, pois n gravam todos os telefones, tipo, se de 100 em 100, na 3 pra 4ª vez, ou seja,
de 300 em diante, a possibilidade do erro surge, se tem 500 pessoas era pra ter 2500 telefones, só q aparecem 4 a menos.
Detalhe: se uso MERGE ao invés de PERSIST, a performance é um pouco menor, mas o erro n acontece.

Pq este erro, alguma ideia?

PARTE DAS ENTIDADES

public class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="pessoa_id")
    private Integer id;

    @Column(name="pessoa_nome", length=50)
    private String nome;

    @Column(name="pessoa_sobrenome", length=50)
    private String sobrenome;

    @OneToMany(mappedBy="pessoa", cascade=CascadeType.ALL)
    private List<Telefone> lsTelefone = new LinkedList<Telefone>();
    

    public void adicionaTelefone(Telefone telefone){
        telefone.setPessoa(this);
        getLsTelefone().add(telefone);

    }
@Entity
@Table(name="cad_telefone")
public class Telefone implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name="telefone_numero")
    private String numero;

    @Column(name="telefone_tipo")
    private String tipo;

    @JoinColumn(name="pessoa_id", referencedColumnName="pessoa_id")
    @ManyToOne(cascade=CascadeType.REFRESH)
    private Pessoa pessoa;
public class Teste {

    EntityManager em = null;
    EntityManagerFactory emf=null;
    
    public Teste(){
        emf=Persistence.createEntityManagerFactory("TesteUnitarioPU");
        em = emf.createEntityManager();
    }

    public void inserePessoa(int registro){

         em.getTransaction().begin();

        for(int i=0; i<registro;i++){

            Pessoa pessoa = new Pessoa();
            pessoa.setNome("João");
            pessoa.setSobrenome("Luis");

           for(int x=0; x<5; x++){

                Telefone telefone = new Telefone();
                telefone.setNumero("1111  " + i);
                telefone.setTipo("Residencial " + x);
                telefone.setPessoa(pessoa);

                pessoa.adicionaTelefone(telefone);
           }

           em.persist(pessoa);
           
        }
         em.getTransaction().commit();
    }
...
Criado 28 de maio de 2009
Respostas 0
Participantes 1