Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.linksinnovation.elearning.model; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; /** * * @author Jirawong Wongdokpuang <jirawong@linksinnovation.com> */ @Entity public class Quiz { @Id @GeneratedValue private Long id; private String question; @OneToMany(cascade = CascadeType.ALL, mappedBy = "quiz") @JsonManagedReference @OrderBy("id ASC") private List<Answer> answers = new ArrayList<>(); @ManyToOne @JsonBackReference private Course course; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } public List<Answer> getAnswers() { return answers; } public void setAnswers(List<Answer> answers) { answers.stream().forEach((ans) -> { ans.setQuiz(this); }); this.answers = answers; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } @Override public int hashCode() { int hash = 7; hash = 37 * hash + Objects.hashCode(this.id); return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Quiz other = (Quiz) obj; if (!Objects.equals(this.id, other.id)) { return false; } return true; } }