How to map two equal entities with Hibernate

Asked

Viewed 171 times

4

I have a scenario where I have the entities Pedido and Usuário. The Pedido is composed of some attributes, among them the requester that is mapped as follows:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "usuario_matricula", referencedColumnName = "matricula", nullable = false)
private Usuario solicitante;

But this request also has the attendant, which is a Usuário. I try to map it like this:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "usuario_matricula", referencedColumnName = "matricula", nullable = true)
private Usuario atendente; 

The problem is that when I try to do it this way I have the following exception:

br.com.Caelum.vraptor.Interceptionexception: org.hibernate.Mappingexception: Repeated column in Mapping for Entity: com.comunicacao.model.Request column: usuario_matricula (should be Mapped with Insert="false" update="false")

If I add what is recommended in the exception (insert="false" update="false") the error goes away. I wonder if this line will allow me to save and edit the data of it and if this is the correct way to map two equal entities in the Hibernate?

1 answer

4


@JoinColumn is used to name the column that holds the foreign key required by the association. If nothing is specified, the field name will be used, then you simply change the name of one of the two Joincolumn to work.

Ex.:

@JoinColumn(name="atendente_matricula")
  • Wow, I forgot the name that would be generated for haha relationship. Thank you very much, that was it.

  • Welcome to Stack Overflow! Welcome to Stackoverflow in English. I edited your post to remove the greetings, as we usually keep them as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the Stack Overflow Chat in Portuguese. If you have questions about the operation, rules and procedures of the site visit the Stack Overflow in English Meta :)

Browser other questions tagged

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