My entity class:

 @Entity
    @Table(catalog = "emp", name = "person")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     */

    @Column(name = "ID", nullable = false,length = 50)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    String id;
    /**
     */

    @Column(name = "ADDRESS", length = 50)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    String address;
    /**
     */

    @Column(name = "EMAIL", length = 50)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    String email;



    @Column(name = "DATE")
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    DateTime date;
    /**
     */

    /*getters and setters */

}

I'm using joda time for my date. When I persist, it throws this exception:

> Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'DATE' at row 1

My Date field contain this value : 2015-09-02T16:24:05.226+04:00

Below is the code where it persist in db:

public Person save(Person person) {

        Person currentPerson = entityManager.find(Person.class,
                person.getId());
        if (currentPerson != null) {
            entityManager.detach(currentPerson);
            person =entityManager.merge(person);
        } else {
            entityManager.persist(person);
        }
        return person;
    }

I am using hibernate/jpa and mysql. My database is created base on my entity class. my date is of type datetime in my entity class, but in database it is of type tinyblob.

How to solve this?

Recommended Answers

All 3 Replies

tiny blob allows up to 256 bytes (unsign). Your string data is 29 characters which may require larger space than that size?

1) Why would you save a date data in tiny blob instead of date itself? If you want it to be encrypted, then alter the database schema to be bigger.
2) What the value do you get when you read it off the database?
3) If you can read from the database, then reverse engineer the portion you read in to save back to the database?

It's not me who put it tinyblob. My database schema is autogenerated by hibernate.

and it's done exactly what you told it to: generate a field that's capable of holding an SQL DATE. Which means a field without a time.
The correct way of indicating a timestamp using JPA is as follows:

   /**
     * Time/date of creation of an entity.
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATION_DATE")
    protected Date creationDate;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.