Save a foreign key entity with Hibernate 4

3

I'm starting to study Hibernate and would like to know how to save a foreign key entity. Here is an example of two entities:

Student:

@Entity(name = "aluno")
public class Aluno extends Pessoa{

    @Column(nullable=false)
    private String matricula;

    @Column(nullable=false)
    private String senha;

    public Aluno(String nome, String sexo, String dataNascimento, 
            String matricula, String senha) {
        super(nome, sexo, dataNascimento);
        this.matricula = matricula;
        this.senha = senha;
    }

Course:

@Entity(name = "curso")
public class Curso {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(nullable=false)
    private String nome;

    @Column(nullable=false)
    private int duracao;

    public Curso(String nome, int duracao) {
        this.nome = nome;
        this.duracao = duracao;
    }

Now the class that contains a foreign key:

@Entity(name = "matricula")
public class Matricula {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne
    @JoinColumn(name = "fk_curso",nullable=false)
    private Curso curso;

    @OneToOne
    @JoinColumn(name = "fk_aluno",nullable=false)
    private Aluno aluno;

    @Column(nullable=false)
    private String matricula;

    public Matricula(Curso curso, Aluno aluno, String matricula) {
        this.curso = curso;
        this.aluno = aluno;
        this.matricula = matricula;
    }

All classes have getters and setters (not put to not get too long)

I would like to know what the "save" method would look like for enrollment. Thank you in advance!

    
asked by anonymous 23.05.2016 / 01:54

2 answers

2

Good morning. The secret to persisting an entity with foreign keys is not in the save method, but in the entity mapping. In order for you to persist all objects by saving only the Enrollment object, you will need to change the Course and Student attribute mapping. It should be informed that when saving the Enrollment object the Course and Student objects will also be saved.

For this you should have the following mapping:

    @OneToOne(cascade=CascadeType.ALL) //Com esse parâmetro na anotação @OneToOne você esta informando para hibernate que toda vez que você salvar uma matricula você vai salvar/Atualizar um Curso 
    @JoinColumn(name = "fk_curso",nullable=false)
    private Curso curso;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "fk_aluno",nullable=false)
    private Aluno aluno;

The "cascade" parameter has other values, so I advise you to read the hibernate doc .

Below is an example of a save method in hibernate.

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     tx.saveOrUpdate(obj);
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

SaveOrUpdate Hibernate

Good luck.

    
23.05.2016 / 14:44
2

Good afternoon everyone, I was having the same problem here!

Solved perfectly:

@OneToOne(cascade=CascadeType.ALL)
    
01.09.2016 / 20:18