Spring Dataの監査機能を使用しており、次のようなクラスがあります。
@実在物
@監査済み
@EntityListeners(AuditingEntityListener.class)
@Table(name="学生")
公開クラス 生徒 {
@ID
@GeneratedValue (戦略 = GenerationType.AUTO)
プライベート ロング ID。
@によって作成された
プライベート文字列の作成者;
@作成日
プライベート作成日;
@LastModifiedBy
プライベート文字列 lastModifiedBy;
@LastModifiedDate
プライベート日付 lastModifiedDate;
...
これで、ドメイン オブジェクトを更新すると、createdBy、createdDate、lastModifiedBy、lastModifiedDate のすべてが正しい値を取得していることがわかるので、監査を適切に構成できたと思います。
ただし、私の問題は、オブジェクトを更新すると、createdBy と createdDate の値が失われることです。そのため、最初にオブジェクトを作成したときは 4 つの値がすべてありますが、更新すると createdBy と createdDate が null になります。また、Hibernate envers を使用してドメイン オブジェクトの履歴を保持しています。
なぜ私がこの振る舞いをするのか知っていますか? ドメイン オブジェクトを更新すると、createdBy と createdDate が空になるのはなぜですか?
更新: @m-deinum の質問に答えるには: はい、Spring Data JPA は正しく構成されています - 他のすべては正常に動作します - 構成を投稿したくありません。
私のAuditorAwareImplはこれです
@成分
public class AuditorAwareImpl は AuditorAware を実装します {
ロガー logger = Logger.getLogger(AuditorAwareImpl.class);
@Autowired
ProfileService プロファイル サービス;
@オーバーライド
public String getCurrentAuditor() {
profileService.getMyUsername(); を返します。
}
}
最後に、更新コントローラーの実装を次に示します。
@Autowired
プライベート StudentFormValidator バリデータ。
@Autowired
プライベート StudentRepository StudentRep;
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String updateFromForm(
@PathVariable("id")長いID、
@Valid Student 生徒、BindingResult 結果、
最終的な RedirectAttributes redirectAttributes) {
学生 s = studentRep.secureFind(id);
if(学生 == null || s == null) {
新しい ResourceNotFoundException() をスローします。
}
validator.validate(学生、結果);
if (result.hasErrors()) {
「学生/フォーム」を返します。
}
Student.setId(id);
Student.setSchool(profileService.getMySchool());
redirectAttributes.addFlashAttribute("メッセージ", "メッセージ");
studentRep.save(学生);
return "redirect:/students/list";
}
更新 2 : 新しいバージョンをご覧ください
@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public ModelAndView editForm(@PathVariable("id")Long id) {
ModelAndView mav = new ModelAndView("学生/フォーム");
学生学生 = studentRep.secureFind(id);
if(学生 == null) {
新しい ResourceNotFoundException() をスローします。
}
mav.getModelMap().addAttribute(学生);
mav.getModelMap().addAttribute("性別", GenderEnum.values());
mav.getModelMap().addAttribute("studentTypes", StudEnum.values());
mav を返します。
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String updateFromForm(
@PathVariable("id")長いID、
@Valid @ModelAttribute Student 生徒、BindingResult 結果、
最終的な RedirectAttributes redirectAttributes、SessionStatus ステータス) {
学生 s = studentRep.secureFind(id);
if(学生 == null || s == null) {
新しい ResourceNotFoundException() をスローします。
}
if (result.hasErrors()) {
「学生/フォーム」を返します。
}
//student.setId(id);
Student.setSchool(profileService.getMySchool());
studentRep.save(学生);
redirectAttributes.addFlashAttribute("message", "Επιτυχής προσθήκη!");
status.setComplete();
return "redirect:/students/list";
}
This still leaves empty the createdBy and createdDate fields when I do an update :(
Also it does not get the School value (which is not contained in my form because it is related to the user currently editing) so I need to get it again from the SecurityContext... Have I done anything wrong ?
Update 3: For reference and to not miss it in the comments: The main problem was that I needed to include the @SessionAttributes annotation to my controller.