다음과 같은 DB 스키마를 Domain(Entity)로 구현해보자
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;
}
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;
}
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<>();
}
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;
}
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;
}