The contents of the database do not change despite the normal communication and modification of the contents to be modified through the DTO.
entity code like this
@Getter
@ToString(callSuper = true)
@Table(indexes = {
@Index(columnList = "title"),
@Index(columnList = "createdAt"),
@Index(columnList = "createdBy")
})
@Entity
public class Article extends AuditingFields{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Setter @Column(nullable = false) private String title;
@Setter @Column(nullable = false, length = 10000) private String content;
@ToString.Exclude
@JoinTable(
name = "article_hashtag",
joinColumns = @JoinColumn(name = "articleId"),
inverseJoinColumns = @JoinColumn(name = "hashtagId")
)
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<Hashtag> hashtags = new LinkedHashSet<>();
@OrderBy("createdAt DESC")
@OneToMany(mappedBy = "article", cascade = CascadeType.ALL)
@ToString.Exclude
private final Set<ArticleComment> articleComments = new LinkedHashSet<>();
@Setter @ManyToOne(optional = false) @JoinColumn(name = "userId")
private UserAccount userAccount; //유저 계정 id
protected Article() {}
public Article(UserAccount userAccount,String title, String content) {
this.userAccount = userAccount;
this.title = title;
this.content = content;
}
public static Article of(UserAccount userAccount, String title, String content) {
return new Article(userAccount, title, content);
}
public void addHashtag(Hashtag hashtag){
this.getHashtags().add(hashtag);
}
public void addHashtags(Collection<Hashtag> hashtags){
this.getHashtags().addAll(hashtags);
}
public void clearHashtags(){
this.getHashtags().clear();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Article article)) return false;
return this.getId() != null && this.getId().equals(article.id);
}
@Override
public int hashCode() {
return Objects.hash(this.getId());
}
}
problem method like this
public void updateArticle(Long articleId, ArticleDto dto) {
try {
Article article = articleRepository.getReferenceById(articleId);
UserAccount userAccount = userAccountRepository.getReferenceById(dto.userAccountDto().userId());
if (article.getUserAccount().equals(userAccount)) {
if (dto.title() != null) {
article.setTitle(dto.title());
}
if (dto.content() != null) {
article.setContent(dto.content());
}
Set<Long> hashtagIds = article.getHashtags().stream()
.map(Hashtag::getId)
.collect(Collectors.toUnmodifiableSet());
article.clearHashtags();
articleRepository.save(article);
articleRepository.flush();
hashtagIds.forEach(hashtagService::deleteHashtagWithoutArticles);
Set<Hashtag> hashtags = renewHashtagsFromContent(dto.content());
article.addHashtags(hashtags);
}
} catch (EntityNotFoundException e) {
log.warn("error", e.getLocalizedMessage());
}
}
Strangely, when you call the "Article" class through the "findById" function, it updates normally
when i call updateMethod, entity modified right away
I guess the cause was related to "lazy loading". Or maybe "Entity Manager" can't do "dirty check"?
I would appreciate it if you could let me know the cause of the above mentioned problem and how to solve it.
Please understand that I may not be able to express my English properly because I am not good at it.