OneToMany一对多

情景:以极简图床为例,图床内有很多相册,每一个相册里对应着一些图片,此时相册为一,图片为多。

代码

  • Album.java(相册类)
import lombok.Data;

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

@Entity
@Data
public class Album {
    @GeneratedValue
    @Id
    private Integer id;
    private String albumCover;
    private String albumTitle;
    private String  albumDescription;
    private Integer likes;

    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
    @JoinColumn(name = "picture_id")

    private List pictureList = new ArrayList<>();

    public Album() {
    }

    public Album(String albumCover, String albumTitle, String albumDescription, Integer likes) {
        this.albumCover = albumCover;
        this.albumTitle = albumTitle;
        this.albumDescription = albumDescription;
        this.likes = likes;
    }
}
  • PictureList.java(相片类)

import io.swagger.models.auth.In;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Data
public class PictureList {
    @GeneratedValue
    @Id
    private Integer id;
    private String pictureTitle;
    private String pictureLink;

    public PictureList(String pictureTitle, String pictureLink) {
        this.pictureTitle = pictureTitle;
        this.pictureRoad = pictureLink;
    }

    public PictureList() {
    }

}

你可能感兴趣的:(OneToMany一对多)