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,
From my angular js front end request is going as :
But my REST call Debug log is as follows :
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
The following error is shown in my browser console.
<changeSet author="mnkb" id="20151008151559">
<addColumn schemaName="sample"
tableName="invoice">
<column name="bill_date" type="timestamp"/>
</addColumn>
</changeSet>
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}
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]
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 + "'" +
'}';
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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??
What does the date get persisted as in the database??
Any update on this?
Do you still require help?
Do you still require help?
ASKER
As I was in travel I did not check this yet, by today I will night I will update it, sorry for the late.
ASKER
If I use java.util.Date, it is working fine, for now, I have removed joda time.
ASKER
java.util.Date has solved the problem.
ASKER
Hi gurpsbassi, In your comments article link is missing, which you were referring. Could you please add it?
ASKER
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:
Open in new window
But in the server log see,
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.