Search code examples
javahibernatejakarta-eeentity

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


Solution

  • 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 operator like below.

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