java.sql.SQLException:解析时间戳时出错

时间:2017-07-05 10:01:46

标签: java hibernate sqlite

我想从SQLite数据库获取带有Hibernate 5的Date对象。我实现了这段代码:

实体:

@Entity
@Table(name = "CLIENT_CARDS")
public class ClientCardsModel implements Serializable
{
    @Id
    @GeneratedValue
    private Integer id;

    @Column
    private String number;

    @Column
    @Type(type = "date")
    @Temporal(TemporalType.DATE)
    private Date added;
     ......
}

查询:

SessionFactory factory = HibernateUtils.getSessionFactory();
        Session session = factory.getCurrentSession();

        try
        {
            session.getTransaction().begin();

            String sql = "Select e from " + ClientCardsModel.class.getName() + " e ";

            Query<ClientCardsModel> query = session.createQuery(sql);
            List<ClientCardsModel> dataList = query.getResultList();

            for (ClientCardsModel card : dataList)
            {
                ClientCardObj obj = new ClientCardObj(card.getId(), card.getNumber(), card.getAdded());
                list.add(obj);
            }
            session.getTransaction().commit();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            session.getTransaction().rollback();
        }

当我执行查询时,我得到错误堆栈:

Caused by: java.sql.SQLException: Error parsing time stamp
    at org.sqlite.jdbc3.JDBC3ResultSet.getDate(JDBC3ResultSet.java:327)
    at org.sqlite.jdbc3.JDBC3ResultSet.getDate(JDBC3ResultSet.java:346)
    at org.hibernate.type.descriptor.sql.DateTypeDescriptor$2.doExtract(DateTypeDescriptor.java:76)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:260)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:256)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:246)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2844)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1741)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1667)
    at org.hibernate.loader.Loader.getRow(Loader.java:1556)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:985)
    at org.hibernate.loader.Loader.doQuery(Loader.java:943)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    at org.hibernate.loader.Loader.doList(Loader.java:2615)
    ... 106 more
Caused by: java.text.ParseException: Unparseable date: "1970-01-01 00:00:00" does not match (\p{Nd}++)\Q-\E(\p{Nd}++)\Q-\E(\p{Nd}++)\Q \E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q.\E(\p{Nd}++)
    at org.sqlite.date.FastDateParser.parse(FastDateParser.java:299)
    at org.sqlite.date.FastDateFormat.parse(FastDateFormat.java:490)

您知道如何解决此问题并正确解析日期吗?

0 个答案:

没有答案