How to structure Hibernate Entity relationship annotations?

Asked

Viewed 1,148 times

3

I would like to understand the difference between Hibernate annotations with JPA: @Onetoone, @Onetomany, @Manytomany and @Manytoone from Hibernate, how it works?

  • 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.

  • Do you want a tutorial on JPA annotations? If you have a more specific question, update the question.

1 answer

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:

devmedia

tutorialspoint

Browser other questions tagged

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