3
I would like to understand the difference between Hibernate annotations with JPA: @Onetoone, @Onetomany, @Manytomany and @Manytoone from Hibernate, how it works?
3
I would like to understand the difference between Hibernate annotations with JPA: @Onetoone, @Onetomany, @Manytomany and @Manytoone from Hibernate, how it works?
7
@One-To-One: The One-to-One association is similar to the Many-to-one association with the difference that the column will be defined as single. Example:
@OneToOne
public Endereco getEndereco() {
return this.endereco;
}
Many-to-One: The Many-to-one association is the most common type of association in which an object can be associated with several 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 got the information:
Browser other questions tagged java hibernate jpa relationship annotation
You are not signed in. Login or sign up in order to post.
I have never worked with Hibernate but these annotations refer to the type of entity-relationship in a database modeling. For example, a Post class has as attributes author, id, content and a list of comments. Comments has the @Onetomany annotation, A post can have multiple comments.
– Daniela Morais
Do you want a tutorial on JPA annotations? If you have a more specific question, update the question.
– Caffé