JPA JPQL DataTypes

KMS·2022년 4월 12일
0

JPA Basics

목록 보기
16/20

ENUM MemberType

public class MemberDTO {
    private Long id;
    private String username;
    private int age;
    private MemberType memberType; //Member에서 memberType을 MemberType으로 지정했으므로, MebmerDTO에서도 MemberTpye으로 지정해줘야 됨

    public MemberDTO() {
    }

    public MemberDTO(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public MemberDTO(Long id, String username, int age) {
        this.id = id;
        this.username = username;
        this.age = age;
    }

    public MemberDTO(Long id, MemberType memberType) {
        this.id = id;
        this.memberType = memberType;
    }

    public MemberDTO(Long id, int age, MemberType memberType) {
        this.id = id;
        this.age = age;
        this.memberType = memberType;
    }

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public int getAge() {
        return age;
    }

    public MemberType getMemberType() {
        return memberType;
    }

    public void setMemberType(MemberType memberType) {
        this.memberType = memberType;
    }
}
			String query = "select new jpql.MemberDTO(m.id, m.age, m.memberType) from Member m" +
                    " where m.memberType = :memType" +
                    " and m.age between 20 and 25" +
                    " and m.username like :userName";
            List<MemberDTO> resultList = em.createQuery(query, MemberDTO.class)
                    .setParameter("memType", MemberType.USER)
                    .setParameter("userName", "%user%")
                    .getResultList();
            for (MemberDTO memberDTO : resultList) {
                System.out.println("memberDTO.getAge() = " + memberDTO.getAge()
                        + ", memberDTO.getMemberType() = " + memberDTO.getMemberType());
            }

결과:
memberDTO.getAge() = 21, memberDTO.getMemberType() = USER
memberDTO.getAge() = 25, memberDTO.getMemberType() = USER

EXISTS, IN
AND, OR, NOT
=, >, >=, <, <=, <>
BETWEEN, LIKE, IS NULL
모두 SQL문과 동일하게 사용 가능

profile
Student at Sejong University Department of Software

0개의 댓글