JPA HSQLDB TIMESTAMP转换

时间:2016-05-08 07:36:03

标签: hibernate jpa hsqldb

在我们的项目中,我们使用的是JPA 2.1,hibernate 5.1,Spring 2.4.6,hsqldb 2.3.2。我们的hsql查询有问题,它给我们错误的localdatetime转换。这是错误: 转换中不兼容的数据类型:从SQL类型TIMESTAMP到[B,值:org.hsqldb.types.TimestampData的实例。这是完整的堆栈跟踪: http://pastebin.com/bhGGpvNS

package calendar.model;

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Basic;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import calendar.converter.LocalDateTimeAttributeConverter;

@Entity
@Table(name = "Event")
public class Event implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer eventId;
    private Integer creatorAccountId;
    private String eventName;
    private String eventStreetAddress;
    private String eventCity;
    private String eventState;

    @Convert(converter = LocalDateTimeAttributeConverter.class)
    private LocalDateTime eventTime;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getEventId() {
        return eventId;
    }

    public void setEventId(Integer eventId) {
        this.eventId = eventId;
    }

    @Basic(optional = false)
    public Integer getCreatorAccountId() {
        return creatorAccountId;
    }

    public void setCreatorAccountId(Integer creatorAccountId) {
        this.creatorAccountId = creatorAccountId;
    }

    @Basic(optional = false)
    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    @Basic(optional = false)
    public String getEventStreetAddress() {
        return eventStreetAddress;
    }

    public void setEventStreetAddress(String eventStreetAddress) {
        this.eventStreetAddress = eventStreetAddress;
    }

    @Basic(optional = false)
    public String getEventCity() {
        return eventCity;
    }

    public void setEventCity(String eventCity) {
        this.eventCity = eventCity;
    }

    @Basic(optional = false)
    public String getEventState() {
        return eventState;
    }

    public void setEventState(String eventState) {
        this.eventState = eventState;
    }

    @Basic(optional = false)
    public LocalDateTime getEventTime() {
        return eventTime;
    }

    public void setEventTime(LocalDateTime eventTime) {
        this.eventTime = eventTime;
    }

    public Event(){}

    public Event(Integer accountId, String name,String streetAddress, String city, String state, LocalDateTime time)
    {
        this.creatorAccountId = accountId;
        this.eventName = name;
        this.eventCity = city;
        this.eventState = state;
        this.eventStreetAddress = streetAddress;
        this.eventTime = time;
    }

    public int compareTo(Event o) {
        // TODO Auto-generated method stub
        return this.getEventTime().compareTo(o.getEventTime());
    }

}

2 个答案:

答案 0 :(得分:5)

我不确定Java 8 Date Time支持是否已添加到Hibernate核心模块中。目前,我认为您还需要为项目添加以下依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version>
</dependency>

上面的内容应该不再需要为java 8 Date / Time类编写自己的AttributeConverters。

答案 1 :(得分:0)

Hibernate没有support the LocalDateTime class。我建议你使用joda或内置的Java日期/日历矩阵Date.from() and friends