ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 고급매핑 - 실전예제
    김영한(인프런 강의)/자바 ORM 표준 JPA 프로그래밍 2020. 10. 22. 13:50
    반응형

    도메인

     

     

     

     

    테이블(싱글테이블 전략 사용함)

     

     

     

    코드

    @Entity
    @DiscriminatorValue("앨범")
    public class Album extends Item{
    
    	private String artist;
    	private String etc;
    
        Getter(), Setter()...
    }

     

    @Entity
    @DiscriminatorValue("영화")
    public class Movie extends Item{
    	
    	private String director;
    	private String actor;
        
        Getter(), Setter()...
    }

     

    @Entity
    @DiscriminatorValue("책")
    public class Book extends Item {
    
    	private String autor;
    	private String isbn;
        
        Getter(), Setter()...
    }

     

    @Entity
    public class Category {
    	@Id
    	@GeneratedValue
    	private Long id;
    	
    	private String name;
    	
    	@ManyToOne
    	@JoinColumn(name = "PARENT_ID")
    	private Category parent;
    	
    	@OneToMany(mappedBy = "parent")
    	private List<Category> child = new ArrayList<>();
    	
    	@ManyToMany
    	@JoinTable(name ="CATEGORY_ITEM", 
    				joinColumns = @JoinColumn(name ="CATEGORY_ID"),
    				inverseJoinColumns = @JoinColumn(name = "ITEM_ID"))
    	private List<Item> items = new ArrayList<>();
    	
    	Getter(), Setter() ...
    }

     

    @Entity
    public class Delivery {
    	@Id
    	@GeneratedValue
    	private Long id;
    
    	private String city;
    	private String street;
    	private String zipcode;
    	private DeliveryStatus status;
    
    	@OneToOne
    	@JoinColumn(name = "delivery")
    	private Orders order;
        
        Getter(), Setter() ...
     }

     

    public enum DeliveryStatus {
    }
    

     

    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn
    public abstract class Item {
    	@Id
    	@GeneratedValue
    	@Column(name = "ITEM_ID")
    	private Long id;
    
    	private String name;
    	private int price;
    	private int stockQuantity;
    
    	@ManyToMany(mappedBy = "items")
    	private List<Category> categories = new ArrayList<>();
        
        Getter(), Setter() ...
    }

     

    @Entity
    public class Member {
    	@Id
    	@GeneratedValue
    	@Column(name = "MEMBER_ID")
    	private Long id;
    	@Column(name = "USERNAME")
    	private String name;
    	private int age;
    	@OneToMany
    	@JoinColumn(name = "member")
    	private List<Order> orders = new ArrayList<>();
        
        Getter(), Setter() ...
    }

     

    @Entity
    @Table(name = "ORDERS")
    public class Order {
    
    	@Id
    	@GeneratedValue
    	@Column(name = "ORDER_ID")
    	private Long id;
    
    	@ManyToOne
    	@JoinColumn(name = "MEMBER_ID")
    	private Member member;
    	
    	@OneToOne
    	@JoinColumn(name = "DELIVERY_ID")
    	private Delivery delivery;
    	
    	@OneToMany(mappedBy = "order")
    	private List<OrderItem> orderItems = new ArrayList<>();
    
    	private LocalDateTime orderDate;
    
    	@Enumerated(EnumType.STRING)
    	private OrderStatus status;
        
        Getter(), Setter() ...
    }

     

    @Entity
    public class OrderItem {
    	@Id
    	@GeneratedValue
    	@Column(name = "ORDER_ITEM_ID")
    	private Long id;
    
    	@ManyToOne
    	@JoinColumn(name = "ORDER_ID")
    	private Order order;
    
    	@ManyToOne
    	@JoinColumn(name = "ITEM_ID")
    	private Item item;
    
    	private int orderPrice;
    	private int count;
        
        Getter(), Setter() ...
    }  

     

    public enum OrderStatus {
    	ORDER, CANCEL
    }

     

     

     

     

    결과

     

    반응형
Designed by Tistory.