Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

Repository 제공 기능 2 본문

JPA

Repository 제공 기능 2

Seung__ 2021. 5. 23. 17:52

1. flush()


flush() : JPA context에 있는 DB값을 DB에 적용하도록 함.

@Test
    @Transactional
    void crud(){//create, read, update, delete

        userRepository.save(new User("new hyun", "newHyun@ddd.com"));

        userRepository.flush();

       userRepository.findAll().forEach(System.out::println);

    }

DB에 잘 반영됨

2. saveAndFlush()


save() 와 flush() 를 합한 method로 추가적인 flush() method 사용 절차가 필요 없다.

@Test
    @Transactional
    void crud(){//create, read, update, delete

        userRepository.saveAndFlush(new User("new hyun", "newHyun@ddd.com"));

        //userRepository.flush();

       userRepository.findAll().forEach(System.out::println);

    }

DB에 잘 반영됨.

 

3. count()


현재 사용가능한 entity의 개수를 long타입으로 return.

  @Test
    void crud(){//create, read, update, delete

       long count = userRepository.count();

        System.out.println("count : " + count);

    }

count결과 = 5

4. existsById()


@Test
    void crud(){//create, read, update, delete

      boolean exists = userRepository.existsById(1L);

        System.out.println("1L있음?: " + exists);

    }

id가 1인 entity 존재

 

5. delete()


id가 1인 entity를 삭제해보자.

id1가 1인 entity를 찾아서 delete method에 넣어보자.

@Test
    void crud(){//create, read, update, delete

      userRepository.delete(userRepository.findById(1L).orElseThrow(RuntimeException::new));

    }

}

delete()에는 entity가 들어가야 하고, findById()는 optional로 return한다. 

값이 없는 경우 .orElseThrow(RuntimeException::new)로 처리한다.

 

아래는 콘솔에 찍인 log이다.

 

select(id 일치하는애 찾기)->select(delete하기 위한 id 일치하는애 찾기)->delete(지우기) 순으로 진행.

Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.created_at as created_2_0_0_,
        user0_.email as email3_0_0_,
        user0_.name as name4_0_0_,
        user0_.updated_at as updated_5_0_0_ 
    from
        user user0_ 
    where
        user0_.id=?
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.created_at as created_2_0_0_,
        user0_.email as email3_0_0_,
        user0_.name as name4_0_0_,
        user0_.updated_at as updated_5_0_0_ 
    from
        user user0_ 
    where
        user0_.id=?
Hibernate: 
    delete 
    from
        user 
    where
        id=?

 

6. deleteById()


위와는 다르게 id로 찾아서 바로 지우면?

@Test
    void crud(){//create, read, update, delete

      userRepository.deleteById(1L);

    }

select --> delete로 진행되었다.

Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.created_at as created_2_0_0_,
        user0_.email as email3_0_0_,
        user0_.name as name4_0_0_,
        user0_.updated_at as updated_5_0_0_ 
    from
        user user0_ 
    where
        user0_.id=?
Hibernate: 
    delete 
    from
        user 
    where
        id=?

id:1 entity가 없어졌당

 

7. deleteAll()


다지워!

 @Test
    void crud(){//create, read, update, delete

      userRepository.deleteAll();

      userRepository.findAll().forEach(System.out::println);
    }

다지워서 결과값이 없슴다.

Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_

 

8. deleteAll(args)


deleteAll에 args를 넣어봅시다.

 

id가  1L, 3L인 entity를 지울 예정

지울 entity의 id를 리스트로 만들어서. findAllById로 찾고 deleteAll에 넣어준다. 

@Test
    void crud(){//create, read, update, delete

      userRepository.deleteAll(userRepository.findAllById(Lists.newArrayList(1L, 3L)));

      userRepository.findAll().forEach(System.out::println);
    }

 

find all: 1번, 3번 찾기 ->delete : 1번 찾기-> delete : 3번 찾기-> delete : 1번 지우고->delete : 3번지움

즉 delete가 두 번 실행됨!

Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_ 
    where
        user0_.id in (
            ? , ?
        )
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.created_at as created_2_0_0_,
        user0_.email as email3_0_0_,
        user0_.name as name4_0_0_,
        user0_.updated_at as updated_5_0_0_ 
    from
        user user0_ 
    where
        user0_.id=?
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.created_at as created_2_0_0_,
        user0_.email as email3_0_0_,
        user0_.name as name4_0_0_,
        user0_.updated_at as updated_5_0_0_ 
    from
        user user0_ 
    where
        user0_.id=?
Hibernate: 
    delete 
    from
        user 
    where
        id=?
Hibernate: 
    delete 
    from
        user 
    where
        id=?
Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_
User(name=park, email=park@google.com, createdAt=2021-05-23T17:32:16.052676, updatedAt=2021-05-23T17:32:16.052676, id=2)
User(name=kim, email=kim@google.com, createdAt=2021-05-23T17:32:16.052676, updatedAt=2021-05-23T17:32:16.052676, id=4)
User(name=hyun, email=hyun@google.com, createdAt=2021-05-23T17:32:16.052676, updatedAt=2021-05-23T17:32:16.052676, id=5)

 

 

9. deleteInBatch()


 @Test
    void crud(){//create, read, update, delete

      userRepository.deleteInBatch(userRepository.findAllById(Lists.newArrayList(1L, 3L)));

      userRepository.findAll().forEach(System.out::println);
    }

 

find all: 1번 3번을 찾고(in)->delete : ( where의 or조건으로 1번 3번 같이 지움) 

즉 delete 1번 실행 및 delete전에 체크하는 select 없음 

entity가 있든 없든 바로 delete 해버림

Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_ 
    where
        user0_.id in (
            ? , ?
        )
Hibernate: 
    delete 
    from
        user 
    where
        id=? 
        or id=?
Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_
User(name=park, email=park@google.com, createdAt=2021-05-23T17:31:20.605567, updatedAt=2021-05-23T17:31:20.605567, id=2)
User(name=kim, email=kim@google.com, createdAt=2021-05-23T17:31:20.606860, updatedAt=2021-05-23T17:31:20.606860, id=4)
User(name=hyun, email=hyun@google.com, createdAt=2021-05-23T17:31:20.606860, updatedAt=2021-05-23T17:31:20.606860, id=5)

 

10. deleteAllInBatch()


찾고 뭐고 없이 그냥 바로 다 지워버림

   @Test
    void crud(){//create, read, update, delete

      userRepository.deleteAllInBatch();
      userRepository.findAll().forEach(System.out::println);
    }

바로 delete해벌임...

Hibernate: 
    delete 
    from
        user
Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.created_at as created_2_0_,
        user0_.email as email3_0_,
        user0_.name as name4_0_,
        user0_.updated_at as updated_5_0_ 
    from
        user user0_

'JPA' 카테고리의 다른 글

SimpleJpaRepository분석  (0) 2021.05.24
Page , Query By Example(QBE)  (0) 2021.05.23
Repository에서 제공하는 기능 예시  (0) 2021.05.23
data.sql작성 및 log조회 방법  (0) 2021.05.23
JPA 설정 및 예시  (0) 2021.05.23
Comments