zam
zam

Reputation: 461

Joda DateTime hibernate mapping spring form

I'm trying to map a DateTime object to a TimeStamp SQL column.

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

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

I'm using Spring form tag to get the user to enter a date input of the type "mm/dd/YYYY HR:MIN AM/PM" as according to @DateTimeFormat(style="SS"). I have imported joda-time-hibernate packages and all other necessary packages

When I submit the form I get the following error: org.joda.time.contrib.hibernate.PersistentDateTime.nullSafeSet(Ljava/sql/PreparedStatement;Ljava/lang/Object;ILorg/hibernate/engine/spi/SessionImplementor;)V

Its clear I'm doing something wrong. Can someone point me in the right direction?

Upvotes: 1

Views: 2455

Answers (2)

Lord Nighton
Lord Nighton

Reputation: 1720

When using Hibernate's 4.3.5.Final version (the last at the post creation time) I faced the same problem. As it was mentioned before, you have to use Jadira Framework

Java-code:

private DateTime creationDate;

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

My pom.xml Hibernate-related section:

<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>

My pom.xml JodaTime-related section:

<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 script that created needed table (pay attention at TIMESTAMP type):

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));

Upvotes: 1

Ilya
Ilya

Reputation: 29741

Looks like you are using Hibernate 4.+.
Joda-Time Hibernate doesn't support this Hibernate version.
You can use usertype from Jadira Framework: PersistentDateTime doc

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

maven dependency

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.jodatime</artifactId>
    <version>2.0.1</version>
</dependency>

Upvotes: 5

Related Questions