maity_bi wrote:
This code maps a HashMap but I would like to do the same using annotations. Is it possible? Clould you help me?
Thaks in advance!!!
You cannot use directly HashMap, but you can use Map:
Code:
//imports
@Entity
@Table(name="Person")
public class Person {
private String name;
private int id;
private Map<String,Phone> phones;
public Person(){}
@Column(name="Nome")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//a Person has many Phones --> one-to-many relationship
@OneToMany
public Map<String, Phone> getPhones() {
return telefoni;
}
}
This is a basic way to use JPA annotations for Maps, and this kind of annotation triggers the creation of a JoinTable. It works for List and Set too.