How to best persist Date property as string property of ejb3 entity?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package entities;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author nikola
*/
@Entity
@Table(name = "CHART")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Chart.findAll", query = "SELECT c FROM Chart c"),
@NamedQuery(name = "Chart.findById", query = "SELECT c FROM Chart c WHERE c.id = :id"),
@NamedQuery(name = "Chart.findByAmount", query = "SELECT c FROM Chart c WHERE c.amount = :amount"),
@NamedQuery(name = "Chart.findByAmountdue", query = "SELECT c FROM Chart c WHERE c.amountdue = :amountdue")
})
public class Chart implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "AMOUNT")
private Double amount;
@Column(name = "AMOUNTDUE")
private Double amountdue;
@Column(name = "date")
private String dateofbirthString;
@javax.persistence.Transient
private Date dateofbirth;
public Chart() {
}
public Chart(Double amount, Double amountdue) {
this.amount = amount;
this.amountdue = amountdue;
this.dateofbirth = new Date();
}
public Chart(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
public Double getAmountdue() {
return amountdue;
}
public void setAmountdue(Double amountdue) {
this.amountdue = amountdue;
}
@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 Chart)) {
return false;
}
Chart other = (Chart) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Chart[ id=" + id + " ]";
}
public Date getDateofbirth() {
return dateofbirth;
}
public void setDateofbirth(Date dateofbirth) {
this.dateofbirth = dateofbirth;
}
@PrePersist
@PreUpdate
public void convertDateToString() {
dateofbirthString = String.valueOf(dateofbirth);
}
}
Entity needs to be sortable by date in tapestry application and ejb session needs to use date for some calculations too?
Actually, it depends on how much information about the dates you plan to store. Usually you use a Date
field with an @Temporal
annotation, defining it as either TemporalType.DATE
, TemporalType.TIME
or TemporalType.TIMESTAMP
. You should go for TIMESTAMP
if you're in for some ordering, changing this:
@Column(name = "date")
private String dateofbirthString;
Into this:
@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date dateofbirth;
Remember to change the accessors accordingly. Another tip: you can map a Calendar
directly instead of a Date
.
我会选择时间戳(即使它是ackwarard以保持字符串长)或选择一种DateFormat预定义格式,以便在字符串和日期类型之间轻松直接地进行转换。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.