How to map two equal entities with Hibernate

4

I have a scenario where I have the entities Pedido and Usuário . The Pedido is composed of some attributes, among them the requestor 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, who 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 this, I have the following exception:

  

br.com.caelum.vraptor.InterceptionException:   org.hibernate.MappingException: Repeated column in mapping for entity:   com.unicacao.model.Pedido column: usuario_matricula (should be   mapped with insert="false" update="false")

If I add what is recommended in the (insert="false" update="false") exception, the error some. I would like to know if this line will allow me to save and edit the data of the same and if this is the correct way to map two equal entities in Hibernate ?

    
asked by anonymous 13.12.2016 / 13:20

1 answer

4

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

Eg:

@JoinColumn(name="atendente_matricula")
    
13.12.2016 / 13:30