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.ropr.model; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; /** * * @author Dominik */ @Entity @Table(name = "user") @XmlRootElement @NamedQueries({ @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"), @NamedQuery(name = "User.findByIdUser", query = "SELECT u FROM User u WHERE u.idUser = :idUser"), @NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name"), @NamedQuery(name = "User.findBySurname", query = "SELECT u FROM User u WHERE u.surname = :surname"), @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"), @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password") }) public class User implements Serializable, Comparable<User> { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "idUser") private Integer idUser; @Size(max = 45) @Expose @Column(name = "name") private String name; @Size(max = 45) @Expose @Column(name = "surname") private String surname; // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation @Basic(optional = false) @NotNull @Expose @Size(min = 1, max = 45) @Column(name = "email") private String email; @Basic(optional = false) @NotNull @Expose @Size(min = 1, max = 45) @SerializedName("pass") @Column(name = "password") private String password; @ManyToMany(mappedBy = "userList", fetch = FetchType.EAGER) private List<Device> deviceList; public User() { } public User(Integer idUser) { this.idUser = idUser; } public User(Integer idUser, String email, String password) { this.idUser = idUser; this.email = email; this.password = password; } public Integer getIdUser() { return idUser; } public void setIdUser(Integer idUser) { this.idUser = idUser; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @XmlTransient public List<Device> getDeviceList() { return deviceList; } public void setDeviceList(List<Device> deviceList) { this.deviceList = deviceList; } @Override public int hashCode() { int hash = 0; hash += (idUser != null ? idUser.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { if (!(object instanceof User)) { return false; } User other = (User) object; if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser))) { return false; } return true; } @Override public String toString() { return "com.model.User[ idUser=" + idUser + " ]"; } public String toJSON() { final GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); final Gson gson = builder.create(); String json = gson.toJson(this); return json; } public static User fromJSON(String json) { final GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); final Gson gson = builder.create(); User u = gson.fromJson(json, User.class); return u; } @Override public int compareTo(User o) { int compareId = o.getIdUser(); return compareId - idUser; } }