01/10/2018, 16:35

Hỏi về Spring (java)

Mình có 1 list các video và bài học, mỗi bài học sẽ có một hoặc nhiều video. Câu hỏi là làm sao để 1 video đó chỉ nằm trong 1 bài học thôi ạ?

anon52681320 viết 18:50 ngày 01/10/2018

Hỏi cấu trúc dữ liệu hay hỏi cách nào giải quyết bằng Spring à ?

Đăng Huy viết 18:44 ngày 01/10/2018

Vâng ạ. Em muốn hỏi giải quyết như thế nào ạ? Hoặc có thể cho em key để em tìm được không ạ?

Lập Trình Sư viết 18:51 ngày 01/10/2018

Nó đại loại là như thế này:

// Lesson.java
@Entity
@Table(name = "lessons")
public class Lesson {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String title;

    @OneToOne(mappedBy = "lessons")
    private Video video;

    // constructors + getters + setters
}


// Video.java
@Entity
@Table(name = "videos")
public class Video {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String title;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "lesson_id")
    private Lesson lesson;

    // constructors + getters + setters
}


// VideoRepository.java
public interface VideoRepository extends JpaRepository<Video, Integer>{
}


// LessonRepository.java
public interface LessonRepository extends JpaRepository<Lesson, Integer>{
}
Đăng Huy viết 18:43 ngày 01/10/2018

Oh. Em cảm ơn bác nhiều ạ. Để em ngâm cứu thêm ạ!

Bài liên quan
0