com.linksinnovation.elearning.model.Topic.java Source code

Java tutorial

Introduction

Here is the source code for com.linksinnovation.elearning.model.Topic.java

Source

/*
 * 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.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Jirawong Wongdokpuang <jirawong@linksinnovation.com>
 */
@Entity
public class Topic {
    @Id
    @GeneratedValue
    private Long id;
    @Column(length = 4000)
    private String message;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createDate;
    @JsonManagedReference
    @OrderBy("id ASC")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "topic", orphanRemoval = true)
    private Set<Reply> replys = new HashSet<>();
    @OneToOne
    private UserDetails user;
    @JsonBackReference
    @ManyToOne
    private Course course;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Set<Reply> getReplys() {
        return replys;
    }

    public void setReplys(Set<Reply> replys) {
        this.replys = replys;
    }

    public void addReply(Reply reply) {
        if (this.replys == null) {
            this.replys = new HashSet<>();
        }
        reply.setTopic(this);
        this.replys.add(reply);
    }

    public void removeReply(Reply reply) {
        this.replys = null;
    }

    public UserDetails getUser() {
        return user;
    }

    public void setUser(UserDetails user) {
        this.user = user;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    @PrePersist
    public void prePersist() {
        this.setCreateDate(new Date());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * 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 Topic other = (Topic) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

}