본문 바로가기
728x90

🔗 JPA7

[JPA] com.querydsl.core.types.ExpressionException:No constructor found for class - with parameters: [에러] com.querydsl.core.types.ExpressionException: No constructor found for class - with parameters:[]... QueryDsl을 이용해서 Entity 에서 전체 필드를 호출하는 것이 아니라, 특정 필드들만 호출하거나, 연관된 다른 Entity의 필드를 동시에 호출하고 싶다면 아래와 같이 Projections.constructor를 사용해야한다. [Repository] jpaQueryFactory .select(Projections.constructor(MemberDTO.class, member.email, order.orderDate, order.status ) ) .from(member) {...} [DTO] @Getter @.. 2022. 10. 29.
OSIV(Open Session in view)이란? 장단점, 써야할지 말아야할지 Open-In-View / Open-Session-In-View / Open-EntityManager-In-View 스프링부트 2.x 이상을 사용 중이라면 아래 경고를 한번 쯤 본 적 있을 것이다. 2022-10-21 15:19:29.799 WARN 45628 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 여기서 .. 2022. 10. 21.
[JPA] 컬렉션 조회(1대다 관계) 최적화 이번엔 저번 포스팅에 이어 주문 api XToMany 관계인 Order(주문)과 OrderItem(주문상품)에 대한 이야기다. XToOne의 경우 fetch join(left outer join)을 하면 성능 최적화가 되는데, XToMany 관계는 join하면 다(1:다) 측 데이터랑 물려서, 데이터가 뻥튀기(?)된다. 이 뻥튀기라는 말은 이따 코드로 볼 수 있겠지만, (예를들어 물건 3개를 산 하나의 주문내역을 확인 할 때, 1개의 결과가 나오는게 아니고 3개의 상품이 연결되어 있어서, 결과가 3개가 나와버리는 상황을 말한다.ㅜㅜ) 그래서 고려할게 많아진다. 일단 간단하게 코드를 살펴보자. 일단 처음에 짠 코드는 다음과 같았다. [Order] public class Order { {...} @OneTo.. 2022. 10. 14.
[JPA] jdbcsqlintegrityconstraintviolationexception null not allowed for column 에러 로그: jdbcsqlintegrityconstraintviolationexception null not allowed for column ... 해결방법: # 1 H2 데이터 베이스 버전 1.4.200 으로 변경. # 2 H2 database GenerationType.IDENTITY 오류 Member.java의 @GeneratedValue(strategy = GenerationType.IDENTITY) 부분을 @GeneratedValue(strategy = GenerationType.SEQUENCE) 로 변경 # 3 H2 2.1.210 버전에서 ;MODE=MySQL 추가 spring.datasource.url=jdbc:h2:tcp://localhost/~/test;MODE=MySQL 2022. 10. 14.
jpa 지연로딩 사용 주의점 (jpa 지연 로딩과 조회 성능 최적화) 주문 API를 만들며 만난 jpa 지연로딩 문제와 주의점. XToOne 성능 최적화에 대한 이야기이다. 우선 주문 API는 다음과 같은 구조다. (전부 XToOne 관계) order - member (ManyToOne) order - delivery (OneToOne) order - orderitem : XToMany @GetMapping("/orders") public List orders() { List all = orderRepository.findAllByString(new OrderSearch()); for (Order order : all) { order.getMember().getName(); order.getDelivery().getStatus(); List orderItems = orde.. 2022. 10. 13.
Setter없이 Entity update 값의 변화점을 예측하기 힘들어지게하는 Setter는 지양해야 한다. 이후 Setter에 의한 문제가 발생할 여지를 남겨두는 것은 좋지 않기 때문이다. 물론 '무조건' 쓰지 말아야 하는 것은 아니다. 단순히 필드 하나만 변경하는 경우라면 setter를 써도 된다. 그럼 Setter없이 어떻게 Entity를 update 할 수 있을까? update가 필요한 값만을 받는 메서드를 따로 생성해 그것을 명시적으로 사용하면 된다. @RestController ... @PostMapping("/update/{id}") public void updateCategory(@PathVariable("id") Long id, @RequestBody CategoryDto categoryDto) { categoryService... 2022. 10. 9.
[JPA] No Property 메소드명 found 에러 해결방법 (No property 'xx' found for type 'x') No property 'xx' found for type 'x' Repository에 만들어둔 메소드명이 Entity에 존재하지 않기 때문에 사용할 수 없다는 에러. 메서드 명 종류가 꽤 많기 때문에 전체 리스트는 아래 링크를 참조해서 고치면 된다. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords Spring Data JPA - Reference Documentation Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRep.. 2022. 10. 1.
728x90