# [JPA] Spring Data JPA ๊ตฌํ˜„์ฒด(SimpleJpaRepository) ๋ถ„์„
Study Repository

[JPA] Spring Data JPA ๊ตฌํ˜„์ฒด(SimpleJpaRepository) ๋ถ„์„

by rlaehddnd0422

์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด SimpleJpaRepository๋ฅผ ๋ถ„์„ํ•ด ๋ด…์‹œ๋‹ค.

@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {

SimpleJpaRepository๋Š” ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA์˜ ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด๋กœ

 

@Repostiory : JPA ์˜ˆ์™ธ๋ฅผ ์Šคํ”„๋ง์ด ์ถ”์ƒํ™”ํ•œ ์˜ˆ์™ธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

@Transactional : ํŠธ๋žœ์žญ์…˜์„ ์ ์šฉํ•ด JPA์˜ ๋ชจ๋“  ๋ณ€๊ฒฝ์„ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ๋™์ž‘ํ•˜๋„๋ก ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

- ๋”ฐ๋ผ์„œ, Spring Data JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋น„์Šค ๊ณ„์ธต์—์„œ ํŠธ๋žœ์žญ์…˜์„ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ํŠธ๋žœ์žญ์…˜์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

- ์‚ฌ์‹ค ๊ตฌํ˜„์ฒด์— @Transactional์ด ๊ฑธ๋ ค ์žˆ๋Š” ๋•๋ถ„์— ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํŠธ๋žœ์žญ์…˜ ์—†์ด๋„ ๋ฐ์ดํ„ฐ ๋“ฑ๋ก, ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

Spring Data JPA ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด์˜ save() ๋ฉ”์†Œ๋“œ

/*
 * (non-Javadoc)
 * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
 */
@Transactional
@Override
public <S extends T> S save(S entity) {

   Assert.notNull(entity, "Entity must not be null.");

   if (entityInformation.isNew(entity)) {
      em.persist(entity);
      return entity;
   } else {
      return em.merge(entity);
   }
}

์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋ฉด persist, ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ mergeํ•˜๋„๋ก ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€ ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋ฅผ ๊ตฌ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ๋ฐ, ๊ธฐ๋ณธ ์ „๋žต์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ ๊ตฌ๋ณ„ ๋ฐฉ๋ฒ• Default 
: ์‹๋ณ„์ž๊ฐ€ ๊ฐ์ฒด์ผ ๋•Œ null ์—ฌ๋ถ€๋กœ ํŒ๋‹จ(null์ธ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ), ์‹๋ณ„์ž๊ฐ€ ์ž๋ฐ” ๊ธฐ๋ณธ ํƒ€์ž…์ผ ๋•Œ ์ˆซ์ž 0 ๊ฐ’์ด๋ฉด ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋กœ ํŒ๋‹จ!

@GeneratedValue ์‚ฌ์šฉ ์‹œ

save()์˜ ๊ฒฝ์šฐ ํ˜ธ์ถœ ์‹œ์ ์— ์‹๋ณ„์ž๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋กœ ํŒ๋‹จํ•ด์„œ ์ •์ƒ๋™์ž‘ํ•˜์ง€๋งŒ,

merge()์˜ ๊ฒฝ์šฐ ์šฐ์„  DB๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ , DB์— ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋กœ ์ธ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ๋งค์šฐ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Persistable์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„  ํŒ๋‹จ ๋กœ์ง์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ ํ™•์ธ ์—ฌ๋ถ€๋ฅผ DB์— ์ ‘๊ทผํ•˜์ง€ ์•Š๊ฒŒ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค.

 

โžก๏ธ @CreatedDate๋ฅผ ์กฐํ•ฉํ•ด์„œ @CreatedDate LocalDateTime createdDate์— ๊ฐ’์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ์—”ํ‹ฐํ‹ฐ๋กœ ํŒ๋‹จํ•˜๋„๋ก ๋ณ€๊ฒฝ 

@Entity
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class) // @CreatedDate ์‚ฌ์šฉ 
public class Item implements Persistable<Long> {
    @Id
    private Long id;
    @CreatedDate 
    private LocalDateTime createdDate;

    public Item(Long id) {
        this.id = id;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return createdDate == null;
    }
}

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ