Daughter entity does not receive its id after merge of the parent entity via @CascadeType

0

I have a problem when I add an entity to the database via Cascade. After the update of entity Aluno the Daughter entity NivelAlunoLinguaEstrangeira does not work correctly.

@Entity
@Table(name = "ed44_aluno")
@SequenceGenerator(name = "ED44_SQC", sequenceName = "ed44_aluno_ed44_cod_aluno_seq", initialValue = 0, allocationSize = 1)
public class Aluno implements AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ED44_SQC")
    @Column(name = "ed44_cod_aluno")
    private Long id;

    @OneToMany(mappedBy = "aluno", cascade = { CascadeType.ALL })
    private List<NivelAlunoLinguaEstrangeira> linguas;

}

@Entity
@Table(name = "ed77_linguagem_aluno")
@SequenceGenerator(name = "ED77_SQC", sequenceName = "s_ed77_linguagem_aluno", initialValue = 0, allocationSize = 1)
public class NivelAlunoLinguaEstrangeira implements AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ED77_SQC")
    @Column(name = "ed77_cod_linguagem")
    private Long id;

    @OneToOne
    @JoinColumn(name = "fked77ed44_cod_aluno")
    private Aluno aluno;

}

    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("post");      
        EntityManager manager = factory.createEntityManager();
        Aluno aluno = manager.find(Aluno.class, 1l);
        aluno.getLinguas().size();
        manager.close();

        NivelAlunoLinguaEstrangeira nivel = new NivelAlunoLinguaEstrangeira();
        nivel.setAluno(aluno);
        aluno.addLingua(nivel);

        manager = factory.createEntityManager();
        manager.getTransaction().begin();
        manager.merge(aluno); //update
        manager.getTransaction().commit();
        manager.close();

        System.out.println(nivel); // nivel.getId() == null WHY?
    }

My output:

  

after find method call:
  select student where id = 1 (edited, long query)

After calling the merge method:

  

select nextval ('s_ed77_linguagem_aluno')

After the commit :

  

insert into ed77_linguagem_aluno (fked77ed44_cod_aluno, fked77tg22_cod_nivel_lingua_ent, fked77tg22_cod_nivel_lingua_esc > > fked77tg22_cod_nivel_lingua_fal, fked77tg22_cod_nivel_lingua_le > > fked77tg33_cod_lingua, ed77_cod_linguagem) values

(,???????)

End of code:

  null, null, null, null, parent = null, null, parent = null, null, parent = null, null, parent = = null, conjuge = null, photoSrc = null, dataCadastro = 2014-05-23 15: 46: 43,586, address = null, contact = null, documentation = null], lingua = null, writes = null, understands = null, speaks = null, read = null]

The problem is that my NivelAlunoLinguaEstrangeira entity does not get its id. This is bad since if I call the MERGE method again it will again add a NivelAlunoLinguaEstrangeira (because it does not have an ID, and pro JPA is a new object).

How to solve this?

    
asked by anonymous 24.05.2014 / 20:51

1 answer

1

Looking at your code, I think it's correct.

But I suggest a change:

public static void main(String[] args) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("post");      
    EntityManager manager = factory.createEntityManager();
    Aluno aluno = manager.find(Aluno.class, 1l);
    aluno.getLinguas().size();
    manager.close();

    NivelAlunoLinguaEstrangeira nivel = new NivelAlunoLinguaEstrangeira();
    nivel.setAluno(aluno);
    aluno.addLingua(nivel); // Não é necessário.

    manager = factory.createEntityManager();
    manager.getTransaction().begin();
    manager.merge(nivel); //insert
    manager.getTransaction().commit();

    // ou, caso queria o objeto atualizado.
    aluno = manager.refresh(aluno);

    manager.close();

    System.out.println(nivel); // nivel.getId() == null WHY?
}

In this case, I believe that the ORM will perform the insertions correctly since the OneToOne mapping is in the NivelAlunoLinguaEstrangeira class, it will insert the foreign key fked77ed44_cod_aluno correctly.

The fact that it does not set the ID is correct since you only merge the aluno . Maybe refresh , in the nivel ed entity in your code can solve the problem.

    
25.05.2014 / 16:47