Domain 구현1

dev_314·2022년 8월 26일
0

JPA - Trial and Error

목록 보기
3/16


다음과 같은 DB 스키마를 Domain(Entity)로 구현해보자

TAG

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

@Entity
@Table(name = "TAG")
@Getter
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "TAG_ID")
    private Long id;

    @Column(name = "TAG", length = 24, nullable = false)
    private Long name;
}

INGREDIENT

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

@Entity
@Table(name = "INGREDIENT")
@Getter
public class Ingredient {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "INGREDIENT_ID")
    private Long id;

    @Column(name = "NAME", length = 30, nullable = false)
    private String name;
}

USER

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "USER")
@Getter
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_ID")
    private Long id;

    @Column(name = "EMAIL", length = 50, nullable = false)
    private String email;

    @Column(name = "PASSWORD", length = 50, nullable = false)
    private String password;

    @Column(name = "NICKNAME", length = 50, nullable = false)
    private String nickName;

    @Column(name = "THUMBNAIL_URL", length = 2048, nullable = false)
    private String thumbnailUrl;

    @Column(name = "DESCRIPTION", length = 900)
    private String description;

    @OneToMany(mappedBy = "user")
    List<Bookmark> bookmarks = new ArrayList<>();

    @OneToMany(mappedBy = "user")
    List<UserIngredient> userIngredients = new ArrayList<>();

    @OneToMany(mappedBy = "user")
    List<Recipe> recipes = new ArrayList<>();
}

USER_INGREDIENT

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

@Entity
@Table(name = "USER_INGREDIENT")
@Getter
public class UserIngredient {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_INGREDIENT_ID")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "USER_ID")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "INGREDIENT_ID")
    private Ingredient ingredient;
}

SUBSCRIBE -> Follow

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

@Entity
@Table(name = "FOLLOW")
@Getter
public class Follow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "FOLLOW_ID")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "STAR_ID")
    private User star;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "FAN_ID")
    private User fan;
}
profile
블로그 이전했습니다 https://dev314.tistory.com/

0개의 댓글