Search code examples
springhibernateone-to-manyjoincolumn

Entity is not being created on database when JoinColumn annotation is added


I have this model class:

public class Usuario {
...
  @OneToMany(fetch = FetchType.EAGER)
  @Fetch(FetchMode.SELECT)
  private List<org.loja.model.credencial.Credencial> credenciais;
...
}

If I add this annotation to this attribute:

@JoinColumn(unique=false)

causes the table usuario_credenciais not being created on database (it is when the annotation is omitted, but causes problems during runtime due to the uniqueness situation).

What I can do to solve that?


Solution

  • I solve that changing the OneToMany annotation to ManyToOne, getting this:

      @ManyToMany(fetch = FetchType.EAGER)
      @JoinTable(name="usuario_credenciais", joinColumns={@JoinColumn(name="usuario_id")}, inverseJoinColumns={@JoinColumn(name="credencial_id")})
      @Fetch(FetchMode.SELECT)
      private List<org.loja.model.credencial.Credencial> credenciais;
    

    Now the table is created on the database, and the application allows multiple users have the same credentials.