아래와 같은 코드를 통해 알아보자.
@Entity @Getter @Setter //setter는 보통 사용하면 안되지만, 예시에서는 편의를 위해 사용
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Sample {
@Id @GeneratedValue
private Long id;
private String title;
private String content;
@Builder
public Sample(String title, String content) {
this.title = title;
this.content = content;
}
}
public interface SampleRepository extends JpaRepository<Sample, Long> {
}
@Test
public void 새로_저장() throws Exception {
//given
Sample sample = new Sample("title", "content");
Sample savedSample = sampleRepository.save(sample);
//when
em.flush();
em.clear();
Optional<Sample> foundSample = sampleRepository.findById(sample.getId());
Optional<Sample> foundSavedSample = sampleRepository.findById(savedSample.getId());
//then
assertThat(foundSample.isPresent()).isTrue();
assertThat(foundSavedSample.isPresent()).isTrue();
assertThat(foundSample.get().getId()).isEqualTo(foundSavedSample.get().getId());
}