스프링부트 연관관계

2021. 3. 19. 01:44Spring Boot

반응형

스프링부트에서 연관관계 매핑하는 방법

 

fk 있는 곳엔 manytoone을 선언!

다른 테이블에서 fk로 사용되는 pk가 있는 테이블(엔티티)엔 onetomany를 선언

 

아래와 같이 선언해주자 

 

 

ex) RecipeInfo

@NoArgsConstructor(access = AccessLevel.PUBLIC) // Unit Test 를 위해 PUBLIC
@EqualsAndHashCode(callSuper = false)
@Data // from lombok
@Entity // 필수, Class 를 Database Table화 해주는 것이다
@Table(name = "RecipeInfo") // Table 이름을 명시해주지 않으면 class 이름을 Table 이름으로 대체한다.
public class RecipeInfo extends BaseEntity {
    @Id // PK를 의미하는 어노테이션
    @Column(name = "recipeId", nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer recipeId;

    @Column(name="recipeNmKo", nullable = false,length = 45)
    private String recipeNmKo;

    @Column(name = "sumry", nullable = false,length = 200)
    private String sumry;

    @Column(name = "nationCode", nullable = false)
    private Integer nationCode;

    @Column(name = "nationNm", nullable = false,length = 20)
    private String nationNm;

    @Column(name = "tyCode", nullable = false)
    private Integer tyCode;

    @Column(name="tyNm", nullable = false, length = 20)
    private String tyNm;

    @Column(name = "cookingTime", nullable = false, length = 20)
    private String cookingTime;

    @Column(name = "calorie", nullable = false, length = 20)
    private String calorie;

    @Column(name = "qnt", nullable = false, length = 20)
    private String qnt;

    @Column(name = "levelNm", nullable = false, length = 20)
    private String levelNm;

    @Column(name = "irdntCode", nullable = false, length = 20)
    private String irdntCode;

    @Column(name = "pcNm", nullable = false, length = 20)
    private String pcNm;

    @Column(name = "imgUrl", nullable = false)
    private String imgUrl;

    @Column(name = "detUrl", nullable = false)
    private String detUrl;

    @Column(name="status", nullable=false, length=10)
    private String status="ACTIVE";

    @OneToMany(mappedBy = "recipeInfo", cascade = CascadeType.ALL)
    private List<ScrapPublic> scrapPublics = new ArrayList<>();

    public RecipeInfo(String recipeNmKo,String sumry,Integer nationCode, String nationNm,Integer tyCode,String tyNm,String cookingTime,String calorie,String qnt, String levelNm,String irdntCode ,
                      String pcNm, String imgUrl, String detUrl){
        this.recipeNmKo = recipeNmKo;
        this.sumry = sumry;
        this.nationCode = nationCode;
        this.nationNm = nationNm;
        this.tyCode = tyCode;
        this.tyNm = tyNm;
        this.cookingTime = cookingTime;
        this.calorie = calorie;
        this.qnt = qnt;
        this.irdntCode = irdntCode;
        this.pcNm = pcNm;
        this.imgUrl = imgUrl;
        this.detUrl = detUrl;
    }

}

 

 

ex) ScrapPublic

@NoArgsConstructor(access = AccessLevel.PUBLIC) // Unit Test 를 위해 PUBLIC
@EqualsAndHashCode(callSuper = false)
@Data // from lombok
@Entity // 필수, Class 를 Database Table화 해주는 것이다
@Table(name = "ScrapPublic") // Table 이름을 명시해주지 않으면 class 이름을 Table 이름으로 대체한다.
public class ScrapPublic extends BaseEntity {
    @Id // PK를 의미하는 어노테이션
    @Column(name = "idx", nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idx;

    //@Column(name="userIdx", nullable = false)
    //private Integer userIdx;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userIdx", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "publicIdx", nullable = false)
    private RecipeInfo recipeInfo;

    @Column(name="status", nullable=false, length=10)
    private String status="ACTIVE";

    public ScrapPublic(User user, RecipeInfo recipeInfo){
        this.user = user;
        this.recipeInfo = recipeInfo;
    }

}

 

 

 

ex) UserInfo

@NoArgsConstructor(access = AccessLevel.PUBLIC) // Unit Test 를 위해 PUBLIC
@EqualsAndHashCode(callSuper = false)
@Data // from lombok
@Entity // 필수, Class 를 Database Table화 해주는 것이다
@Table(name = "UserInfo") // Table 이름을 명시해주지 않으면 class 이름을 Table 이름으로 대체한다.
public class User extends BaseEntity {
    @Id // PK를 의미하는 어노테이션
    @Column(name = "userIdx", nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userIdx;

    @Column(name="socialId", nullable = false, length=45)
    private String socialId;

    @Column(name = "profilePhoto", nullable = false)
    private String profilePhoto;

    @Column(name = "userName", nullable = false, length = 45)
    private String userName;

    @Column(name = "email", length = 45)
    private String email;

    @Column(name = "phoneNumber", nullable = false, length = 45)
    private String phoneNumber;

    @Column(name="status", nullable=false, length=10)
    private String status="ACTIVE";

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<UserRecipe> userRecipes = new ArrayList<>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<ScrapPublic> scrapPublics = new ArrayList<>();

    public User(String socialId, String profilePhoto, String userName, String email, String phoneNumber){
        this.socialId = socialId;
        this.profilePhoto = profilePhoto;
        this.userName = userName;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

}

 

 

ex) UserRecipe

@NoArgsConstructor(access = AccessLevel.PUBLIC) // Unit Test 를 위해 PUBLIC
@EqualsAndHashCode(callSuper = false)
@Data // from lombok
@Entity // 필수, Class 를 Database Table화 해주는 것이다
@Table(name = "UserRecipe") // Table 이름을 명시해주지 않으면 class 이름을 Table 이름으로 대체한다.
public class UserRecipe extends BaseEntity {
    @Id // PK를 의미하는 어노테이션
    @Column(name = "userRecipeIdx", nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userRecipeIdx;

    //@Column(name="userIdx", nullable = false)
    //private Integer userIdx;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userIdx", nullable = false)
    private User user;

    @Column(name = "thumbnail")
    private String thumbnail;

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

    @Column(name = "content",nullable = false, length = 1000)
    private String content;


    @Column(name="status", nullable=false, length=10)
    private String status="ACTIVE";

    public UserRecipe(User user, String thumbnail, String title, String content){
        this.user = user;
        this.thumbnail = thumbnail;
        this.title = title;
        this.content = content;
    }
}
반응형