chalk.domain.Message.java Source code

Java tutorial

Introduction

Here is the source code for chalk.domain.Message.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 chalk.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.*;

/**
 *
 * @author Joris
 */
@Entity
@NamedQueries({ @NamedQuery(name = "Message.getAll", query = "select m from Message as m"),
        @NamedQuery(name = "Message.count", query = "select count(m) from Message as m"),
        @NamedQuery(name = "Message.findAllFor", query = "select m from Message as m where m.sender = :sender and m.receiver = :receiver"),
        @NamedQuery(name = "Message.findAllNew", query = "select m from Message as m where m.receiver = :receiver and m.timeSent > :time") })
public class Message implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne(cascade = CascadeType.MERGE)
    private User sender;
    @ManyToOne(cascade = CascadeType.MERGE)
    private User receiver;
    private String text;
    private Timestamp timeSent;

    public Message(User sender, User receiver, String text) {
        this.sender = sender;
        this.receiver = receiver;
        this.text = text;
        java.util.Date now = new java.util.Date();
        this.timeSent = new Timestamp(now.getTime());
    }

    public Message() {

    }

    public Long getId() {
        return id;
    }

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

    public User getSender() {
        return sender;
    }

    public void setSender(User sender) {
        this.sender = sender;
    }

    public User getReceiver() {
        return receiver;
    }

    public void setReceiver(User receiver) {
        this.receiver = receiver;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Timestamp getTimeSent() {
        return timeSent;
    }

    public void setTimeSent(Timestamp timeSent) {
        this.timeSent = timeSent;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Message)) {
            return false;
        }
        Message other = (Message) object;
        return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
    }

    @Override
    public String toString() {
        return "chalk.domain.Message[ id=" + id + " ]";
    }

}