OS : MacOs Mojave
DB : MySQL 5.7
DB Tool : Sequel Pro
Framework : Spring Boot 2.0
You can see conclusion at bottom of content
1. Preparation
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "uuid")
private String uuid;
@Column(name = "name")
private String name;
}
2. Compare save and saveAndFlush without @Transactional
1. After save() or saveAndFlush(). change value by setMethod
I tried to put breakpoint on the return statement.
1. Change value after save()
public Member createMember(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.save(member);
member.setName("ChangeName");
return member;
}
Hibernate:
insert
into
member
(created_at, last_login, name, uuid)
values
(?, ?, ?, ?)
Changing name is not worked
2. Change value after saveAndFlush()
public Member createMember2(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush");
return member;
}
Changing name is not worked
2. After save() or saveAndFlush(). change value by setMethod. and save() or saveAndFlush() again
3. save() -> change value -> save()
public Member createMember(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.save(member);
member.setName("ChangeName");
return memberRepository.save(member);
}
Hibernate:
4. saveAndFlush() -> change value -> saveAndFlush()
public Member createMember2(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush");
return memberRepository.saveAndFlush(member);
}
3. Compare save and saveAndFlush with @Transactional
1. After save() or saveAndFlush(). change value by setMethod
1. save() -> change value
@Transactional
public Member createMember(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.save(member);
member.setName("ChangeName");
return member;
}
Hibernate:
1. saveAndFlush() -> change value
@Transactional
public Member createMember2(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush");
return member;
}
Hibernate:
2. After save() or saveAndFlush(). change value by setMethod. and save() or saveAndFlush() again
3. save() -> change value -> save()
@Transactional
public Member createMember(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.save(member);
member.setName("ChangeName");
member = memberRepository.save(member);
member.setName("ChangeName2");
member = memberRepository.save(member);
member.setName("ChangeName3");
return member;
}
Hibernate:
insert
into
member
(created_at, last_login, name, uuid)
values
(?, ?, ?, ?)
No output / DB wasn't updated
No output / DB wasn't updated
Hibernate:
update
member
set
created_at=?,
last_login=?,
name=?,
uuid=?
where
id=?
4. saveAndFlush() -> change value -> saveAndFlush()
@Transactional
public Member createMember2(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush");
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush2");
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush3");
return member;
}
public Member createMember2(MemberRequest request) {
Member member = Member.create(request);
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush");
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush2");
member = memberRepository.saveAndFlush(member);
member.setName("ChangeNameFlush3");
return member;
}
Hibernate:
insert
into
member
(created_at, last_login, name, uuid)
values
(?, ?, ?, ?)
Hibernate:
update
member
set
created_at=?,
last_login=?,
name=?,
uuid=?
where
id=?
Hibernate:
update
member
set
created_at=?,
last_login=?,
name=?,
uuid=?
where
id=?
Conclusion
Bye!
'JPA' 카테고리의 다른 글
[JPA] JPAQuery 에서 Pageable(offset,limit,orderby) 사용하기 (0) | 2023.06.21 |
---|---|
[JPA] @Convert 사용해서 Object를 Json 형식으로 저장하기 (0) | 2019.12.11 |
[JPA] save 와 saveAndFlush의 차이 (1) | 2018.11.10 |
[JPA] JPA시작하기 - JPA란? (1) | 2017.02.02 |