-
Spring & JPA persist 자동 생성 id 받기Java&Spring/Spring 2022. 8. 26. 21:11
스프링 persist 이해 하기 .
@RequiredArgsConstructor public class PlaceRepository { @Autowired private final EntityManager em; public Place post(Place place) { em.persist(place); System.out.println(place.getId()); // make point return place; }
Place class 의 instance place 를 매개 변수로 받아 persist 를 하면,
id 를 받는다.. ? -> flush 를 해줘야 한다.
@Repository @RequiredArgsConstructor public class PlaceRepository { @Autowired private final EntityManager em; public Place post(Place place) { em.persist(place); em.flush(); System.out.println(place.getId()); return place; }
id가 생성이 되는걸 볼수 있다.
만약 Generationtype이 IDENTITY 인경우 , mysql id 에 Ai 를 체크 표시 해주거나.
@Entity @Getter @Setter public class Place { @Id @GeneratedValue(strategy = GenerationType.IDEnTIty) @Column(name = "p_id") private long id; private String p_cate; private String p_name; private String p_weekt;
다음과 같이 auto 로 바꿔 준다.
@Entity @Getter @Setter public class Place { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "p_id") private long id; private String p_cate; private String p_name; private String p_weekt;
'Java&Spring > Spring' 카테고리의 다른 글
@Qualifier (0) 2022.10.31 Spring data Jpa camelCase 자동 underscore 방지 Mysql (0) 2022.09.19 Spring Test 기초[1] , assertall() (0) 2022.09.06 JPA 영속성 컨텍스트 (0) 2022.09.01 스프링 커맨드로 빌드 (0) 2022.06.30