DevBoi

[JPA] JPAUpdateClause 사용 본문

Develop/[JPA]

[JPA] JPAUpdateClause 사용

HiSmith 2023. 9. 2. 16:21
반응형

귀찮기도 하지만, 여러개 만드는 것보다 널처리해서 선택적 동적 쿼리를 생성

 

public Long patchMember(MemberDto tMember) {
    JPAUpdateClause clause = queryFactory.update(member);
    clause = MemberUpdateClause(clause,tMember);
    return clause.execute();

  }
JPAUpdateClause MemberUpdateClause(JPAUpdateClause jpaUpdateClause,MemberDto tMember){
    if(member.address != null)
      jpaUpdateClause.set(member.address,tMember.address());
    if(member.birth != null)
      jpaUpdateClause.set(member.birth,tMember.birth());
    if(member.gender != null)
      jpaUpdateClause.set(member.gender,tMember.gender());
    if(member.email != null)
      jpaUpdateClause.set(member.email,tMember.email());
    if(member.introduce != null)
      jpaUpdateClause.set(member.introduce,tMember.introduce());
    if(member.memberIdImageUrl != null)
      jpaUpdateClause.set(member.memberIdImageUrl,tMember.memberIdImageUrl());
    if(member.name != null)
      jpaUpdateClause.set(member.name,tMember.name());
    if(member.status != null)
      jpaUpdateClause.set(member.status,tMember.status());
    if(member.phone != null)
      jpaUpdateClause.set(member.phone,tMember.phone());
    if(member.nickname != null)
      jpaUpdateClause.set(member.nickname,tMember.nickname());
    if(member.nickname != null)
    jpaUpdateClause.where(member.memberId.eq(tMember.memberId()));
    return jpaUpdateClause;
  }

 

위 보다, 더 좋은 방향도 있긴하지만 직관적인게 때로는 더 편할 때도 있다

무튼 공통 동적 모듈로 뺴서 해당 부분을 사용

반응형

'Develop > [JPA]' 카테고리의 다른 글

[QueryDsl] Spring 3 버전 p6spy 적용  (0) 2023.09.09
[JPA] Querydsl & Paging  (0) 2023.09.02
[Jpa] Querydsl build Setting  (0) 2023.09.02
[JPA] ElementCollection  (0) 2023.08.28
[JPA] QueryDsl LeftJoin,SelfJoin  (0) 2023.08.19