Relationship of classes by collections in JPA2

2

In a bean that has a OneToMany link, with others, through a HashMap what annotations should be used?

  @Entity
  class Rodada{
      public HashMap<Pergunta,Resposta> perguntas;
    
asked by anonymous 29.09.2014 / 21:27

1 answer

2

This relationship would be best structured within a wrapper class for these two entities:

@Entity
public class Questao {

   @Id
   private int id;
   @OneToOne
   private Pergunta pergunta;
   @OneToOne
   private Resposta resposta;

   // getter, setter, hashCode, equals

}

So in your round class:

@Entity
public class Rodada {
  //Id omitido
  @OneToMany
  @JoinColumn(name = "rodada_id")
  private Set<Questao> questoes;

  // getter, setter, hashCode, equals  

}

If you still want to keep it as it is, here is an example taken from SOen that may help you:

@ElementCollection
@CollectionTable(name="<name_of_join_table>")
@MapKeyColumn(name="<name_of_map_key_in_table>")
Map<String, Person> personMap;
    
30.09.2014 / 01:30