简体   繁体   中英

hibernate selects on relation one to many

I am developing a jee project using eclispe sts and maven

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

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

<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

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.

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

Note : Ensure that if an object is Collection then you need to iterate it.

You are getting com.stock.mvc.entities.Category@b01648 because this is category object, so you can access its field by using . dot . dot operator like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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