How to structure the hibernate Entity relationship annotations?

3

I would like to understand the difference between hibernate annotations with JPA: @OneToOne, @OneToMany, @ManyToMany and @ManyToOne of hibernate, how does it work?     

asked by anonymous 22.07.2015 / 18:59

1 answer

7

@ One-To-One: The One-to-One membership is similar to many-to-one membership with the difference that the column will be set to unique. Example:

@OneToOne 
public Endereco getEndereco() { 
     return this.endereco; 
}
Many-to-One: Many-to-one association is the most common type of association in which an object can be associated with multiple objects. Example:

@ManyToOne @JoinColumn(name = "publicador_id") 
public Publicador getPublicador() {
    return publicador; 
}

One-to-Many: The @OneToMany annotation can be applied to a field or property of a collection or an array representing the "many" of the association. Example:

@OneToMany(cascade = ALL, mappedBy = "publicador") 
public Set<Livro> getLivros() { 
     return livros; 
}

Many-ToMany: Example:

@ManyToMany(cascade = ALL) 
public Set<Autor> getAutores() { 
     return autores; 
}

Links where I removed information:

devmedia

tutorialspoint

    
23.07.2015 / 14:58