Home > Back-end >  JPA repo is not fetching fresh Data when calling findAllById the previous data is only present in th
JPA repo is not fetching fresh Data when calling findAllById the previous data is only present in th

Time:03-23

Am trying to fetch category wise questions and pass the categoryList to another object called Location like below

Location:
{
 List<Category> categoryList;
}

Category:
{
 List<Question> questions;
 Status status;
}
for (Category category : categories) {
    List<Question> questions = new ArrayList<>();
    questions = questionRepo.findAllById(category.getQuestionIds());
    category.setQuestions(questions);
    category.setStatus(updateQuestionStatus(questions));
    catList.add(category);
}

While trying to fetch the data from a table in loop. The JPA repo's findAllById() is returning the correct data in the first iteration.Where in my DB the status will always be null. I fetch the questions list and based on some logic i update the status to INPROGRESS or TODO

In the next Iteration instead of fetching a new set of data its just returning the previously created instance of the questions variable where the status is already set from the previous iteration instead of null.

So in the end say all the five Location objects having the same CategoryList's questions.

I debugged my code and zeroed in on this line the repo query call, which is Spring JPA not my implementation.

questions = questionRepo.findAllById(category.getQuestionIds());

In the line before this on debug the questions is [] as expected. But when this line is executed i get a copy of the previously altered questions. with status as INPROGRESS or TODO that i set.

I tried lots of things like addAll into a new list etc still no luck. Please help. Thanks in Advance.

UPDATE:

Sharing the Question Entity for better help.

@Entity
@Table(name = "m_question")
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(value = Include.NON_NULL)
@JsonIgnoreProperties(value = { "addedBy", "addedOn", "updatedBy", "updatedOn","deleted" })
public class Question extends Auditable<String> {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "m_question_id")
    private int mQuestionId;
    
    @Column(name = "m_q_category_id")
    private int mQCategoryId;
    
    @Column(name = "category_code")
    private String categoryCode;
    
    @Column(name = "m_q_section_id")
    private int mQSectionId;
    
    @Column(name = "section_code")
    private String sectionCode;
    
    @Column(name = "question_display")
    private String questionDisplay;
    
    @Column(name = "max_mark")
    private int maxMark;
    
    @Column(name = "more_info")
    private String moreInfo;
    
    @Column(name = "special_info")
    private String specialInfo;
    
    @Column(name = "option_type")
    private String optionType;
    
    @Column(name = "display_order")
    private Integer displayOrder;
    
    @Column(name = "is_deleted")
    private boolean isDeleted;
    
    @Column(name = "question_condition")
    private String questionCondition;
    
    @Transient
    private QuestionStatus status;
    
    @Transient
    @JsonSerialize
    @JsonDeserialize
    private List<Option> options;
}

So as shown above the Question entity doesnt change much in table values. But when i fetch those questions i check if any options relevant to the question are present and update the status field to TODO or INPROGRESS which should initially be null but not.

I even tried fetching all the questions (around 500) of them initially and putting them in a map and fetching when required from the map still the same issue is there.

Am not sure what I am doing wrong here.

CodePudding user response:

I was able to fix the issue by clearing the entity manager right before the findAllById call. This seems to break the reference to the previously fetched objects allowing me to add new instances to every element in the loop.

Though am not facing any other issues in the code or object retrieval/persist due to this, i am not 100% sure that this is the correct solution.

For now my issue is solved.

@Autowired
EntityManager em;

for (Category category : categories) {
    List<Question> questions = new ArrayList<>();
    //Clearing the entityManager instance.
    em.clear();
    questions = questionRepo.findAllById(category.getQuestionIds());
    category.setQuestions(questions);
    category.setStatus(updateQuestionStatus(questions));
    catList.add(category);
}

Any better solutions or implementations welcome. Thanks.

  • Related