简体   繁体   English

休眠选择一对多关系

[英]hibernate selects on relation one to many

I am developing a jee project using eclispe sts and maven 我正在使用eclispe sts和maven开发一个jee项目

I have a relation with category parent of article, after adding some article with correct parents id (checked in the database) when I select the list of all articles with their category I obtain a reference to the entity as follows com.stock.mvc.entities.Category@b01648 在选择了具有正确类别ID的所有文章(在数据库中选中)后,当我选择了具有其类别的所有文章列表时,我与文章的类别父类别有关系,我获得了对该实体的引用,如下com.stock.mvc。实体。类别@ b01648

any idea ? 任何想法 ?

this is the relation category article in the entity article 这是实体文章中的关系类别文章

  @ManyToOne
  @JoinColumn(name = "idCategory")  
  private Category category;

and the relation article category in the entity category 以及实体类别中的相关文章类别

  @OneToMany(mappedBy ="category")
  private List<Article> articles;

this is the table of articles in the view article.jsp 这是视图article.jsp中的文章表

<td>${article.getCodeArticle() }</td>                
<td>${article.getDesignation() }</td>                
<td>${article.getPrixUnitaireHt() }</td>                 
<td>${article.getTauxTva() }</td>                
<td>${article.getPrixUnitaireTTC() }</td>                
<td>${article.getCategory() }</td>

the last line (${article.getCategory() }) displays the following message instead of the category id com.stock.mvc.entities.Category@b01648 最后一行($ {article.getCategory()})显示以下消息,而不是类别ID com.stock.mvc.entities.Category@b01648

I am not sure about your entities because you havent posted here. 我不确定您的实体,因为您尚未在此处发布。

But one thing which is visible here is you are accessing values using getters which is wrong way.Instead you can access values by field names like below. 但是这里可见的一件事是您使用getter访问值是错误的方式,而是可以通过如下所示的字段名称访问值。

<td>${article.category}</td>

Note : Ensure that if an object is Collection then you need to iterate it. 注意:确保如果对象是Collection,则需要对其进行迭代。

You are getting com.stock.mvc.entities.Category@b01648 because this is category object, so you can access its field by using . dot 您将收到com.stock.mvc.entities.Category@b01648因为这是类别对象,因此您可以使用来访问其字段. dot . dot operator like below. . dot运算符如下。

 <td>${category.name}</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM