使用注释从休眠中的其他表映射数据?

时间:2019-01-27 11:15:03

标签: java hibernate hashmap annotations

我有如下的数据库方案:

enter image description here

在我的Commande Java类中,我有这个名字(这个问题省略了无用的数据):

@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;
  }
...

我希望在读取数据时,将我所有的“ detail_commande”数据都存储在哈希图中

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   |  

如果我想读取命令号为0的数据,则必须为:

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

但是我不明白注释能做到这一点。

我尝试thisthis

1 个答案:

答案 0 :(得分:1)

您需要在详细信息HashMap上使用@ManyToMany批注,该批注将成为文章列表。阅读本文以了解:https://www.baeldung.com/hibernate-many-to-many