1

我在 Oracle(10g) 数据库的表中有一个日期字段,该字段在TemporalType.TIMESTAMPHibernate 中映射,例如,

@Column(name = "DISCOUNT_START_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date discountStartDate;

//Setter and getter.

它获取日期并显示如下(已插入数据库),

2012-08-29 01:53:10.0
2012-08-20 02:32:22.0
2012-08-01 14:00:21.0
2012-08-20 13:58:01.0
2012-08-30 04:14:13.0
2012-09-10 16:13:45.0

当我尝试使用 JSTL 显示它们时,

<fmt:formatDate pattern="dd-MMM-yyyy hh:mm:ss" value="${row.discountStartDate}"/>

它抛出一个异常,

javax.el.E​​LException:无法将 java.lang.String 类型的 2012-08-29 01:53:10.0 转换为 java.util.Date 类

我试图改变TemporalType.TIMESTAMPTemporalType.DATE没有工作。

早些时候,它使用 XML 映射文件 (xxx.hbm.xml) 之类的。

<property name="discountStartDate" type="date">
  <column length="7" name="DISCOUNT_START_DATE"/>
</property>

但是有了注释,它失败了。如何应用这种格式dd-MMM-yyyy hh:mm:ss来使用 JSTL 在 JSP 中显示这些日期?


编辑:

下面是与Oracle中Discount的表映射的实体类(和方法已省略)。DSCOUNTequals()toString()

@Entity
@Table(name = "DISCOUNT", catalog = "", schema = "WAGAFASHIONDB")
public class Discount  implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "DISCOUNT_ID", nullable = false, precision = 35, scale = 0)
    @SequenceGenerator(name = "discountIdSequence", sequenceName = "DISCOUNT_SEQ", allocationSize=1, initialValue=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "discountIdSequence")
    private Long discountId;

    @Column(name = "DISCOUNT_CODE", length = 100)
    private String discountCode;

    @Column(name = "DISCOUNT_PER", precision = 35, scale = 2)
    private double discountPer;

    @Column(name = "DISCOUNT_START_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date discountStartDate;

    @Column(name = "DISCOUNT_END_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date discountEndDate;

    @OneToMany(mappedBy = "discountId", fetch = FetchType.LAZY)
    private Set<OrderTable> orderTableSet;

    public Discount() {
    }

    public Discount(Long discountId) {
        this.discountId = discountId;
    }

    public Long getDiscountId() {
        return discountId;
    }

    public void setDiscountId(Long discountId) {
        this.discountId = discountId;
    }

    public String getDiscountCode() {
        return discountCode;
    }

    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    public double getDiscountPer() {
        return discountPer;
    }

    public void setDiscountPer(double discountPer) {
        this.discountPer = discountPer;
    }

    public Date getDiscountStartDate() {
        return discountStartDate;
    }

    public void setDiscountStartDate(Date discountStartDate) {
        this.discountStartDate = discountStartDate;
    }

    public Date getDiscountEndDate() {
        return discountEndDate;
    }

    public void setDiscountEndDate(Date discountEndDate) {
        this.discountEndDate = discountEndDate;
    }

    public Set<OrderTable> getOrderTableSet() {
        return orderTableSet;
    }

    public void setOrderTableSet(Set<OrderTable> orderTableSet) {
        this.orderTableSet = orderTableSet;
    }
}

在 JSP 中,使用以下循环来显示数据(降低代码复杂度)。

<c:forEach var="row" items="${list}" varStatus="loop">
    <fmt:formatDate pattern="dd-MMM-yyyy hh:mm:ss" value="${row.discountStartDate}"/>
</c:forEach>

而在 Spring DAO 中,通过前面的循环迭代的列表被简单地检索如下。

@SuppressWarnings("unchecked")
public List<Discount>getList(int currentPage, int rowsPerPage)
{
    List<Discount> list = sessionFactory.getCurrentSession()
                          .createQuery("from Discount order by discountId desc")
                          .setFirstResult(currentPage)
                          .setMaxResults(rowsPerPage).list();

    for(Discount d:list)
    {
       System.out.println(d.getDiscountStartDate()+" : "+d.getDiscountEndDate());
    }
    return list;
}

前面的循环只是为了演示。它显示表中的日期,如下所示。

2012-08-29 01:53:10.0 : 2012-08-31 01:53:16.0
2012-08-20 02:32:22.0 : 2012-08-24 02:34:36.0
2012-08-01 14:00:21.0 : 2012-08-31 14:01:30.0
2012-08-20 13:58:01.0 : 2012-08-21 13:58:20.0
2012-08-30 04:14:13.0 : 2012-11-23 16:21:57.0
2012-09-10 16:13:45.0 : 2012-10-26 16:13:39.0
2012-08-22 16:06:23.0 : 2012-08-15 16:06:17.0
2012-08-22 10:35:04.0 : 2012-08-17 10:34:56.0
2012-08-17 10:35:29.0 : 2012-08-10 10:35:35.0
2012-10-08 10:35:56.0 : 2013-03-08 10:35:49.0
4

0 回答 0