0

如何最好地将日期属性保留为 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);
    }
}

实体需要在挂毯应用程序中按日期排序,并且 ejb 会话也需要使用日期进行某些计算?

4

2 回答 2

2

实际上,这取决于您计划存储多少有关日期的信息。通常您使用Date带有@Temporal注释的字段,将其定义TemporalType.DATETemporalType.TIMETemporalType.TIMESTAMPTIMESTAMP如果您要订购,您应该去,改变这个:

@Column(name = "date")
private String dateofbirthString;

进入这个:

@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date dateofbirth;

请记住相应地更改访问器。另一个提示:您可以Calendar直接映射 a 而不是 a Date

于 2013-05-20T19:04:33.037 回答
0

我会选择时间戳(即使将其保持为 String 的长度是公认的)或选择 DateFormat 预定义格式之一,以便在 String 和 Date 类型之间进行简单直接的转换。

于 2013-05-20T19:07:00.233 回答