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 com.ropr.modelCo.ContactCo; import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; 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.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; 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 = "contact") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Contact.findAll", query = "SELECT c FROM Contact c"), @NamedQuery(name = "Contact.findByIdContact", query = "SELECT c FROM Contact c WHERE c.idContact = :idContact"), @NamedQuery(name = "Contact.findByFirstName", query = "SELECT c FROM Contact c WHERE c.firstName = :firstName"), @NamedQuery(name = "Contact.findByLastName", query = "SELECT c FROM Contact c WHERE c.lastName = :lastName"), @NamedQuery(name = "Contact.findByPhone", query = "SELECT c FROM Contact c WHERE c.phonenumberid.number = :number AND c.deviceid = :device"), @NamedQuery(name = "Contact.findByNick", query = "SELECT c FROM Contact c WHERE c.nick = :nick"), @NamedQuery(name = "Contact.findByEmail", query = "SELECT c FROM Contact c WHERE c.email = :email") }) public class Contact implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "idContact") private Integer idContact; @Size(max = 45) @Expose @Column(name = "firstName") private String firstName; @Size(max = 45) @Expose @Column(name = "lastName") private String lastName; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Expose @Column(name = "nick") private String nick; // @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 @Size(max = 45) @Expose @Column(name = "email") private String email; @JoinColumn(name = "Device_id", referencedColumnName = "idDevice") @ManyToOne(optional = false) @Expose @SerializedName("device") private Device deviceid; @JoinColumn(name = "Phonenumber_id", referencedColumnName = "idPhone") @Expose @SerializedName("phone") @ManyToOne(optional = false) private Phonenumber phonenumberid; @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "contactidContact", fetch = FetchType.EAGER) private List<Message> messageList; public Contact() { } public Contact(ContactCo co) { this.email = co.getEmail(); this.firstName = co.getFirstName(); this.lastName = co.getLastName(); this.nick = co.getNick(); this.phonenumberid = co.getRealNumber(); this.deviceid = co.getDevice(); } public Contact(Integer idContact) { this.idContact = idContact; } public Contact(Integer idContact, String nick) { this.idContact = idContact; this.nick = nick; } public Integer getIdContact() { return idContact; } public void setIdContact(Integer idContact) { this.idContact = idContact; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Device getDeviceid() { return deviceid; } public void setDeviceid(Device deviceid) { this.deviceid = deviceid; } public Phonenumber getPhonenumberid() { return phonenumberid; } public void setPhonenumberid(Phonenumber phonenumberid) { this.phonenumberid = phonenumberid; } @XmlTransient public List<Message> getMessageList() { return messageList; } public void setMessageList(List<Message> messageList) { this.messageList = messageList; } @Override public int hashCode() { int hash = 0; hash += (idContact != null ? idContact.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { if (!(object instanceof Contact)) { return false; } Contact other = (Contact) object; if ((this.idContact == null && other.idContact != null) || (this.idContact != null && !this.idContact.equals(other.idContact))) { return false; } return true; } public String toJSON() { final GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); final Gson gson = builder.create(); String json = gson.toJson(this); return json; } public static Contact fromJSON(String json) { final GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); final Gson gson = builder.create(); Contact u = gson.fromJson(json, Contact.class); return u; } @Override public String toString() { return "com.model.Contact[ idContact=" + idContact + " ]"; } }