Domain 구현2

dev_314·2022년 8월 26일
0

JPA - Trial and Error

목록 보기
4/16

| Domain 구현1에 이어서 나머지 Domain들을 구현하자

RECIPE

JPA

package wonju.howcook.domain;

import lombok.Getter;

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

@Entity
@Table(name = "RECIPE")
@Getter
public class Recipe {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "RECIPE_ID")
    private Long id;

    @Column(name = "DESCRIPTION", length = 450, nullable = false)
    private String description;

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

    @Column(name = "TITLE", length = 90, nullable = false)
    private String title;

    @Column(name = "REFERENCE_URL", length = 2048, nullable = false)
    private String referenceUrl;

    @Column(name = "SERVING", nullable = false)
    @Enumerated(value = EnumType.STRING)
    private Serving serving;

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


    @OneToMany(mappedBy = "recipe")
    private List<RecipeIngredient> recipeIngredients = new ArrayList<>();

    @OneToMany(mappedBy = "recipe")
    private List<RecipeDescription> recipeDescriptions = new ArrayList<>();
}

TypeORM (Node.js)


export default class Recipe {
	...
@ManyToMany(() => User, (user) => user.bookmarks, { lazy: true, nullable: false })
likeUsers: User[];
	...
}


export default class User {
	...
@ManyToMany(() => Recipe, (recipe) => recipe.likeUsers, { lazy: true, nullable: false })
@JoinTable({ name: 'BOOKMARK', joinColumn: { name: 'USER_ID' }, inverseJoinColumn: { name: 'RECIPE_ID' } })
bookmarks: Recipe[];
	...
}
  1. ManyToMany 관계를 OneToMany, ManyToOne 관계로 개선하려고 했는데 연관관계를 잘못 설정했는지 잘 안됐다.
    그래서 그때는 일단 ManyToMany로 해결했다 -> 단점: 추가 컬럼 설정 불가능
  2. 왜 그랬는지는 모르겠는데, TypeORM을 사용했을 때는 양방향 연관관계로 설정했다. JPA는 단방향으로 설계했다. (TypeORM도 단방향이 가능하다)

RECIPE_DESCRIPTION

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

@Entity
@Table(name = "RECIPE_DESCRIPTION")
@Getter
public class RecipeDescription {

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

    @Column(name = "IMAGE_DESCRIPTION", length = 900, nullable = false)
    private String imageDescription;

    @Column(name = "IMAGE_URL", length = 2048, nullable = false)
    private String imageUrl;

    @Column(name = "DESCRIPTION_ORDER", nullable = false)
    private byte descriptionOrder;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "RECIPE_ID")
    private Recipe recipe;
}

RECIPE_TAG

package wonju.howcook.domain;

import lombok.Getter;

import javax.persistence.*;

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

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "RECIPE_ID")
    private Recipe recipe;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "TAG_ID")
    private Tag tag;
}

RECIPE_INGREDIENT

package wonju.howcook.domain;

import lombok.Getter;
import org.hibernate.annotations.ColumnDefault;

import javax.persistence.*;

@Entity
@Table(name = "RECIPE_INGREDIENT")
@Getter
public class RecipeIngredient {

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

    @Column(name = "QUANTITY", nullable = false)
    @ColumnDefault(value = "0")
    private int amount;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "RECIPE_ID")
    private Recipe recipe;

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

BOOKMAKR

package wonju.howcook.domain;

import javax.persistence.*;

@Entity
@Table(name = "BOOKMARK")
public class Bookmark {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "BOOKMAKR_ID")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "USER_ID")
    private User user;

    @ManyToOne(optional = false)
    @JoinColumn(name = "RECIPE_ID")
    private Recipe recipe;
}
profile
블로그 이전했습니다 https://dev314.tistory.com/

0개의 댓글