1

我正在尝试将 DateTime 对象映射到 TimeStamp SQL 列。

import org.springframework.format.annotation.DateTimeFormat;
...

    @Column
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    @DateTimeFormat(style="SS")
    private DateTime dateSmoked;

我正在使用 Spring 表单标签让用户根据@DateTimeFormat(style="SS") 输入“mm/dd/YYYY HR:MIN AM/PM”类型的日期输入。我已经导入了 joda-time-hibernate 包和所有其他必要的包

当我提交表单时,我收到以下错误:org.joda.time.contrib.hibernate.PersistentDateTime.nullSafeSet(Ljava/sql/PreparedStatement;Ljava/lang/Object;ILorg/hibernate/engine/spi/SessionImplementor;)V

很明显我做错了什么。有人可以指出我正确的方向吗?

4

2 回答 2

5

看起来您正在使用Hibernate 4.+
Joda-Time Hibernate不支持这个 Hibernate 版本。
您可以使用以下用户类型Jadira FrameworkPersistentDateTime doc

    @Column
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @DateTimeFormat(style="SS")
    private DateTime dateSmoked;

maven依赖

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.jodatime</artifactId>
    <version>2.0.1</version>
</dependency>
于 2013-09-14T22:06:28.117 回答
1

当使用 Hibernate 的4.3.5.Final版本时(帖子创建时的最后一个版本),我遇到了同样的问题。如前所述,您必须使用Jadira Framework

Java代码:

private DateTime creationDate;

@Column(name = "CREATION_DATE")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getCreationDate() {
    return creationDate;
}

我的pom.xml休眠相关部分:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.3.5.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.5.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate.common</groupId>
  <artifactId>hibernate-commons-annotations</artifactId>
  <version>4.0.4.Final</version>
</dependency>

我的pom.xmlJodaTime 相关部分:

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.jadira.usertype</groupId>
  <artifactId>usertype.jodatime</artifactId>
  <version>2.0.1</version>
</dependency>

创建所需表的 SQL 脚本(注意TIMESTAMP类型):

CREATE TABLE dropbox.MAPPING (ID INT NOT NULL AUTO_INCREMENT, DROPBOX_URL VARCHAR(300) NOT NULL, SHORTENED_URL VARCHAR(30) NOT NULL, CREATION_DATE TIMESTAMP NOT NULL, UNIQUE UQ_MAPPING (ID), PRIMARY KEY (ID));
于 2015-05-31T19:58:03.137 回答