파게로그

실습 4 매핑 및 JPQL 본문

콤퓨타 왕기초/JPA

실습 4 매핑 및 JPQL

파게 2021. 6. 2. 15:55

Member.java

package com.ddoongi.web.entity;

import javax.persistence.*;
import java.util.Date;

@Entity // name 속성을 통해 JPQL에서도 다르게 쓰일 수 있다.
public class Member {

    @Id @GeneratedValue
    private long id;

    private String name;

    private int age;

    @Column(name = "reg_date") @Temporal(TemporalType.DATE)
    private Date regDate;

    @Enumerated(EnumType.STRING)
    private Tier tier;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private Team team;

    public Member() { }

    public Member(String name, int age, Date regDate, Tier tier, Team team) {
        this.name = name;
        this.age = age;
        this.regDate = regDate;
        this.tier = tier;
        this.team = team;
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    public Tier getTier() {
        return tier;
    }

    public void setTier(Tier tier) {
        this.tier = tier;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    @Override
    public String toString() {
        return "Member{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", regDate=" + regDate +
                ", tier=" + tier +
                ", team=" + team +
                '}';
    }
}

 

Team.java

package com.ddoongi.web.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Team {

    @Id @GeneratedValue
    private long id;

    private String name;

    @OneToMany(mappedBy = "team")
    private List<Member> members = new ArrayList<>();

    public Team() { }

    public Team(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Member> getMembers() {
        return members;
    }

    public void setMembers(List<Member> members) {
        this.members = members;
    }

    @Override
    public String toString() {
        return "Team{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

 

Tier.java

package com.ddoongi.web.entity;

public enum Tier {
    DIAMOND,
    PLATINUM,
    GOLD,
    SILVER,
    BRONZE
}

 

Main.java

package com.ddoongi.web;

import com.ddoongi.web.entity.Member;
import com.ddoongi.web.entity.Team;
import com.ddoongi.web.entity.Tier;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");

        EntityManager em = emf.createEntityManager();

        EntityTransaction tx = em.getTransaction();

        tx.begin();

        try {
            Team team = new Team("Blue");
            em.persist(team);

            Member member1 = new Member("Alice", 22, new Date(), Tier.GOLD, team);
            em.persist(member1);

            Member member2 = new Member("Bianca", 23, new Date(), Tier.DIAMOND, team);
            em.persist(member2);

            Member member3 = new Member("Simon", 21, new Date(), Tier.GOLD, team);
            em.persist(member3);

            em.flush();
            em.clear();

            /* team.getMembers() */
            Member findMember = em.find(Member.class, member1.getId());
            Team findTeam = findMember.getTeam();

            List<Member> members = findTeam.getMembers();
            for (Member m : members) {
                System.out.println(m);
            }

            /* JPQL */
            String jpql = "SELECT m FROM Member m WHERE m.name LIKE '%e%'";
            List<Member> result = em.createQuery(jpql, Member.class).getResultList();
            for (Member m : result) {
                System.out.println(m);
            }

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally {
            em.close();
        }

        emf.close();
    }
}

'콤퓨타 왕기초 > JPA' 카테고리의 다른 글

JPA와 객체지향 쿼리  (0) 2021.06.02
JPA 내부 구조  (0) 2021.06.02
연관관계 매핑  (0) 2021.05.28
실습 3 (Mapping annotations)  (0) 2021.05.27
[실습] Mapping to DDL  (0) 2021.05.27
Comments