Link to home
Start Free TrialLog in
Avatar of Too Smart
Too Smart

asked on

problem with mysql time stamp field with Spring Data jpa Date time type while inserting the record into the database

I have added new field of type time stamp using liquibase for my Spring Data Jpa, Angulars and mysql based project as,

 
  <changeSet author="mnkb" id="20151008151559">
           <addColumn schemaName="sample"
                tableName="invoice">       
             <column name="bill_date" type="timestamp"/>
        </addColumn>
    </changeSet>

Open in new window


From my angular js front end request is going as :
   
{"accountNumber":"AC6434364","invoiceNumber":"IN67347643","invoiceAmount":326,"status":"open","ediNumber":"6565EDI","billDate":"2012-05-02T17:07:00.000Z","id":null}

Open in new window


But my REST call Debug log is as follows :


 
   [DEBUG] com.sample.aop.logging.LoggingAspect - Enter: com.sample.web.rest.InvoiceResource.create() with argument[s] = [Invoice{id=null, accountNumber='AC6434364', invoiceNumber='IN67347643', invoiceAmount='326.0', ediNumber='6565EDI', status='open', billDate='2012-05-02T22:37:00.000+05:30'}]
    [DEBUG] com.sample.web.rest.InvoiceResource - REST request to save Invoice : Invoice{id=null, accountNumber='AC6434364', invoiceNumber='IN67347643', invoiceAmount='326.0', ediNumber='6565EDI', status='open', billDate='2012-05-02T22:37:00.000+05:30'}
    Hibernate: insert into invoice (account_number, bill_date, edi_number, invoice_amount, invoice_number, status) values (?, ?, ?, ?, ?, ?)
    [WARN] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1292, SQLState: 22001
    [ERROR] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x16org.joda.time.DateTime\xB8<xdj[\xDD\xF9\x02\x00\x00xr\x00\x1Forg.joda.time.base.BaseDateTime\xFF\xFF\x' for column 'bill_date' at row 1
    [ERROR] com.sample.aop.logging.LoggingAspect - Exception in com.sample.web.rest.InvoiceResource.create() with cause = org.hibernate.exception.DataException: could not execute statement
    org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
    	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.7.RELEASE.jar:4.1.7.RELEASE]

Open in new window



Due to this error this particular record is not getting inserted, earlier when I don't have billDate, the CRUD operation is working fine.

And in Invoce.java, I have
    import org.hibernate.annotations.Cache;
    import org.hibernate.annotations.CacheConcurrencyStrategy;
    import org.joda.time.DateTime;
    
    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    import java.util.Objects;
    
     @Column(name = "bill_date")
        private DateTime billDate;
    
    
    		public DateTime getBillDate() {
    			return billDate;
    		}
    
    		public void setBillDate(DateTime billDate) {
    			this.billDate = billDate;
    		}
    
    @Override
        public String toString() {
            return "Invoice{" +
                    "id=" + id +
                    ", accountNumber='" + accountNumber + "'" +
                    ", invoiceNumber='" + invoiceNumber + "'" +
                    ", invoiceAmount='" + invoiceAmount + "'" +
                    ", ediNumber='" + ediNumber + "'" +
                    ", status='" + status + "'" +
                    ", billDate='" + billDate + "'" +
                    '}';
        }

Open in new window


The following error is shown in my browser console.
    POST http://localhost:8080/api/invoices?cacheBuster=1444330431628 500 (Internal Server Error)

    timestamp: 1444330431767, status: 500, error: "Internal Server Error",…}
    error: "Internal Server Error"
    exception: "org.springframework.dao.DataIntegrityViolationException"
    message: "could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement"
    path: "/api/invoices"
    status: 500
    timestamp: 1444330431767

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Too Smart
Too Smart

ASKER

Yes that is only the problem but if I change it to java.util.Date and my column to

 @Column(name = "bill_date")
    private Date billDate;

Open in new window


in Invoice.java, the accurate date is not getting stored.

For example from the front end console I have the following log:
{"accountNumber":"TRE765656","invoiceNumber":"IVN63565","invoiceAmount":3333333,"status":"Open","ediNumber":"DE524324345","billDate":"2014-10-10T03:25:00.000Z","id":null}

Open in new window


But in the server log   see,

[DEBUG] com.trace.aop.logging.LoggingAspect - Exit: com.sample.web.rest.InvoiceResource.create() with result = <201 Created,Invoice{id=16, accountNumber='TRE765656', invoiceNumber='IVN63565', invoiceAmount='3333333.0', ediNumber='DE524324345', status='Open', billDate='Fri Oct 10 08:55:00 IST 2014'},{Location=[/api/invoices/16], X-traceApp-alert=[traceApp.invoice.created], X-traceApp-params=[16]}>

Open in new window


How can i retain the date as it is as I have entered. or as you suggested is there any simple way/reference to have joda time converter.
or Do you have any other better solution for me to solve this?
I don;t know what is generating your log statements.

What does the date get persisted as in the database??
Any update on this?
Do you still require help?
As I was in travel I did not check this yet, by today I will night I will update it, sorry for the late.
If I use java.util.Date, it is working fine, for now, I have removed joda time.
java.util.Date has solved the problem.
However If you really want to use Joda time, you can still do it with a custom convertor.

For example look at this article.

Although its using the Java 8 java.time API the same principle applies if you wish to use it with joda.
I would prefer to use the convertor approach described in the article
Hi gurpsbassi, In your comments article link is missing, which you were referring. Could you please add it?