spring data jpa : Persistable
This commit is contained in:
@@ -36,7 +36,7 @@ public class MemberController {
|
|||||||
return memberRepository.findAll(pageable).map(MemberDto::new);
|
return memberRepository.findAll(pageable).map(MemberDto::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
// @PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
memberRepository.save(new Member("member" + i, i));
|
memberRepository.save(new Member("member" + i, i));
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.example.springdatajpa.entity;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
|
import org.springframework.data.domain.Persistable;
|
||||||
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.EntityListeners;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||||
|
public class Item implements Persistable<String> {
|
||||||
|
|
||||||
|
// @GeneratedValue 사용하지 않을 경우 -> jpa 기본전략에 의해 persist 가 아닌 merge 수행
|
||||||
|
@Id
|
||||||
|
private String itemId;
|
||||||
|
|
||||||
|
public Item(String itemId) {
|
||||||
|
this.itemId = itemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@CreatedDate
|
||||||
|
private LocalDateTime createdDate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getId() {
|
||||||
|
return itemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNew() {
|
||||||
|
// 새로운 객체인지 판단을 생성 날짜 유무로 한다.
|
||||||
|
return createdDate == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.springdatajpa.repository;
|
||||||
|
|
||||||
|
import com.example.springdatajpa.entity.Item;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface ItemRepository extends JpaRepository<Item, String> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.springdatajpa.entity;
|
||||||
|
|
||||||
|
import com.example.springdatajpa.repository.ItemRepository;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class ItemTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ItemRepository itemRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void save() {
|
||||||
|
itemRepository.save(new Item("A"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user