Daughter entity does not receive its parent entity merge id via @Cascadetype

Asked

Viewed 409 times

0

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

@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 method merge:

select nextval ('s_ed77_student language_student')

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:

International [id=null, student=Student [id=1, codigoInep=null, codigoNis=null, name=SO EDITED 8, surname=null, birth=null, nDependent=null, mae=null, father=null, conjuge=null, fotoSrc=null, dateCadastro=2014-05-23 15:46:43.586, addressco=null, contact=null, documentation=null], language=null, write=null, understand=null, speak=null, read=null]

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

How to solve this?

1 answer

1

Looking at your code, I think that’s correct.

But I suggest an amendment:

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 given that the mapping OneToOne is in class NivelAlunoLinguaEstrangeira, it will do the foreign key insertion fked77ed44_cod_aluno correctly.

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

Browser other questions tagged

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