https://github.com/SY97P/springboot-jpa.git
@Email
어노테이션@Pattern
으로 정규식 적용@Min
, @Max
, @Positive
로 유효값 선언main 패키지에 선언하면 테스트용 어노테이션 import 불가
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@DataJpaTest
@ActiveProfiles("test")
@TestPropertySource(locations = "classpath:application-test.yaml")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public @interface JpaTest {
}
RuntimeException
을 그대로 보내기보단, 좀 더 구체적인 예외 던지기NoSuchElementException
보안 이슈
중복 혹은 충돌
가능성일단 엔티티 null 체크
엔티티가 새로 생성되어야 하는지 체크
프록시로 타입 받아, entityManager (영속성 컨텍스트)에서 찾음
엔티티가 영속성 컨텍스트에 존재하지 않으면 아무 동작 안 함
영속성 컨텍스트에 엔티티가 있으면 영속, 준영속 구분없이 다 가져와서 제거
@Override
@Transactional
@SuppressWarnings("unchecked")
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null");
if (entityInformation.isNew(entity)) {
return;
}
Class<?> type = ProxyUtils.getUserClass(entity);
T existing = (T) em.find(type, entityInformation.getId(entity));
// if the entity to be deleted doesn't exist, delete is a NOOP
if (existing == null) {
return;
}
em.remove(em.contains(entity) ? entity : em.merge(entity));
}
스칼라 타입으로 조회
select o.id, o.name from Order p;
읽기 전용 쿼리 힌트 사용
읽기 전용 트랜잭션 사용
@Transactional(readOnly = true)
@Transcational
선언@Embedded
- @Embeddable
@Embedded
@Embeddable
@Column
으로 Entity설정해도 적용되지 않음@Column(nullable = false)
이런 식으로 선언해야 함