springframework/Spring Data JPA

Spring Data JPA : Enumeration Mapping

Jungsoomin :) 2020. 11. 19. 21:14

엔티티 맵핑 시 Enum 타입 맵핑 방법

  • @Enumerated 사용
  • 기본 값EnumType.ORDINAL , 즉 순번으로 맵핑 된다.
  • Enum 타입의 순번이 바뀔 경우 참사가 일어난다.
  • 그러므로 @Enmerated 의 속성EnumType.STRING 으로 하는 것이 아주아주 안전하다.
public enum AccountStatus {
    BIRTH , LIVE , DIED
}

@Entity
public class Account {

    @Id @GeneratedValue
    private Long id;

    private String username;

    private String password;

    private int age;
    @Enumerated(value = EnumType.STRING)
    private AccountStatus accountStatus;

    public AccountStatus getAccountStatus() {
        return accountStatus;
    }

    public void setAccountStatus(AccountStatus accountStatus) {
        this.accountStatus = accountStatus;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", accountStatus=" + accountStatus +
                '}';
    }
}

///
@RunWith(SpringRunner.class)
@DataJpaTest
public class EnumeratedTest {

    @Autowired
    private AccountRepository accountRepository;

    @Test
    public void test(){
        Account account = new Account();
        account.setUsername("Soomin");
        account.setPassword("Jung");
        account.setAge(28);
        account.setAccountStatus(AccountStatus.LIVE);


        Account savedAccount = accountRepository.save(account);

        Optional<Account> byUsername_opt = accountRepository.findByUsername(savedAccount.getUsername());
        assertThat(byUsername_opt).isNotEmpty();
        Account account1 = byUsername_opt.get();
        assertThat(account1.getPassword()).isEqualTo("Jung");
        assertThat(account1.getAccountStatus()).isEqualTo(AccountStatus.LIVE);

        System.out.println(account1.toString());
    }
}

///
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        account
        (account_status, age, password, username, id) 
    values
        (?, ?, ?, ?, ?)
2020-11-19 21:09:37.376 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [LIVE]
2020-11-19 21:09:37.378 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [28]
2020-11-19 21:09:37.378 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [Jung]
2020-11-19 21:09:37.378 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [Soomin]
2020-11-19 21:09:37.380 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [BIGINT] - [1]
Hibernate: 
    select
        account0_.id as id1_0_,
        account0_.account_status as account_2_0_,
        account0_.age as age3_0_,
        account0_.password as password4_0_,
        account0_.username as username5_0_ 
    from
        account account0_ 
    where
        account0_.username=?
2020-11-19 21:09:37.394 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Soomin]
2020-11-19 21:09:37.400 TRACE 8904 --- [           main] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([id1_0_] : [BIGINT]) - [1]
Account{id=1, username='Soomin', password='Jung', age=28, accountStatus=LIVE}