Note that there are some explanatory texts on larger screens.

plurals
  1. POBi-directional one-to-many relationship does not work
    primarykey
    data
    text
    <p>I have made an application that displays a lot of questions from my database. For this I have made a question entity. I want to be able to "report" a question for being poor/good and so on, so for this I made a feedback entity. </p> <p>The relationship between these would be: one question may have many feedbacks, and one feedback belongs to one question.</p> <p>The problem is that when I save the question feedback instance it all maps perfectly in the database, but when I open a question and loops through all the feedbacks none of the feedbacks added is displayed. In order to have them displayed I need to re-deploy the web application. </p> <p>Why does this happen? </p> <p><strong>For readability I only show the parts involved</strong></p> <p><strong>QuestionFeedback entity</strong></p> <pre><code>public class QuestionFeedback implements Serializable { @ManyToOne private Question question; .... public void setQuestion(Question question) { this.question = question; if (!question.getFeedbacks().contains(this)) { question.getFeedbacks().add(this); } } .... } </code></pre> <p><strong>Question entity</strong></p> <pre><code>@Entity public class Question implements Serializable { @OneToMany(mappedBy = "question", fetch = FetchType.EAGER) private List&lt;QuestionFeedback&gt; feedbacks; public Question() { feedbacks = new ArrayList&lt;QuestionFeedback&gt;(); } public void addFeedback(QuestionFeedback questionFeedback) { if (!getFeedbacks().contains(questionFeedback)) { getFeedbacks().add(questionFeedback); } if (questionFeedback.getQuestion() != this) { questionFeedback.setQuestion(this); } } } </code></pre> <p><strong>Backing bean for the report page</strong></p> <p>The question entity is already retrieved from the database.</p> <pre><code>public String flag() { questionFeedback.setQuestion(question); questionFeedbackService.persist(questionFeedback); return "index"; } </code></pre> <p><strong>DAO class</strong></p> <pre><code>public void persist(QuestionFeedback questionFeedback) { entityManager.persist(questionFeedback); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload