如何最好地将Date属性保存为ejb3实体的字符串属性?
/*
* 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);
}
}
实体需要在tapestry应用程序中按日期排序,ejb会话也需要使用日期进行一些计算吗?
答案 0 :(得分:2)
实际上,这取决于您计划存储的日期的信息量。通常,您使用带有Date
注释的@Temporal
字段,将其定义为TemporalType.DATE
,TemporalType.TIME
或TemporalType.TIMESTAMP
。如果您正在进行某些订购,那么您应该去TIMESTAMP
,改变这一点:
@Column(name = "date")
private String dateofbirthString;
进入这个:
@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date dateofbirth;
请记住相应地更改访问者。另一个提示:您可以直接映射Calendar
而不是Date
。
答案 1 :(得分:0)
我会选择时间戳(即使它是ackwarard以保持字符串长)或选择一种DateFormat预定义格式,以便在字符串和日期类型之间轻松直接地进行转换。