charles Lgn
charles Lgn

Reputation: 528

Map data from other table in hibernate using annotations?

I have Data Base Scheme like below:

enter image description here

and in my Commande java class I have that (omited useless data for this question) :

@Entity
@Table(name = "commande")
public class Commande {
  private int id;
  private Timestamp date;
  private Person client;
  private Magasin shop;
  private HashMap<Article,Integer> detail;

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

...

  public HashMap<Article,Integer> getDetail() {
    return detail;
  }

  public void setDetail(HashMap<Article,Integer> detail) {
    this.detail = detail;
  }
...

I want that when I read data, I got all my "detail_commande" data in a hashmap

detail_commande                      commande
|id_commande|id_article|qqte_cmde|   |id|  date  |id_person|id_shop|
|===========|==========|=========|   |==|========|=========|=======|
|     0     |     0    |    15   |   | 0|1-2-2019|    0    |   2   |
|     0     |     2    |     5   |  
|     0     |     4    |     1   |  

If I want to read data of the command number 0 it must be :

id     : 0
date   : 1-2-2019
person : personData
shop   : shopData
datail : [articleData0 -> 15,
          articleData2 -> 5,
          articleData4 -> 1 ]

But I don't understand annotation to do that.

I try this and this

Upvotes: 1

Views: 57

Answers (1)

Binary Igor
Binary Igor

Reputation: 168

You need to use @ManyToMany annotation on detail HashMap which will become a List of Article. Read this article to understand: https://www.baeldung.com/hibernate-many-to-many

Upvotes: 1

Related Questions