我正在嘗試獲取類別明智的問題并將 categoryList 傳遞給另一個名為 Location 的物件,如下所示
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);
}
在嘗試從回圈中的表中獲取資料時。JPA存盤庫findAllById()在第一次迭代中回傳正確的資料。在我的資料庫中,狀態將始終為null. 我獲取問題串列并根據一些邏輯將狀態更新為INPROGRESS或TODO
在下一次迭代中,它不是獲取一組新資料,而是回傳先前創建的questions變數實體,其中狀態已經從上一次迭代中設定,而不是null.
所以最后說出所有五個具有相同 CategoryList 問題的 Location 物件。
我除錯了我的代碼并將 repo 查詢呼叫歸零,這是 Spring JPA 而不是我的實作。
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.
uj5u.com熱心網友回復:
我能夠通過在findAllById通話前清除物體管理器來解決此問題。這似乎打破了對先前獲取的物件的參考,允許我向回圈中的每個元素添加新實體。
盡管因此在代碼或物件檢索/持久性方面沒有遇到任何其他問題,但我不能 100% 確定這是正確的解決方案。
現在我的問題已經解決了。
@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);
}
歡迎任何更好的解決方案或實作。謝謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448516.html
標籤:java spring-boot hibernate jpa spring-data-jpa
上一篇:多個彈簧啟動物體上的自動增量ID
