将值插入mySql表记录时的jhipster日期时间列问题

时间:2015-10-08 19:28:27

标签: hibernate datetime spring-data-jpa jhipster

我为现有的jhipster实体添加了liquibase类型时间戳的新字段,后端是mysql。

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

从我的角度js前端请求如下:

{"accountNumber":"AC6434364","invoiceNumber":"IN67347643","invoiceAmount":326,"status":"open","ediNumber":"6565EDI","billDate":"2012-05-02T17:07:00.000Z","id":null}

我的REST调用Debug日志如下:

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

由于此错误,此特定记录未插入,之前当我没有billDate CRUD操作正常工作时。

在Invoce.java中,我有

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 + "'" +
                '}';
    }

以下内容显示在我的浏览器控制台中。

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

任何人都可以帮我解决这个billDate插入失败问题吗?

1 个答案:

答案 0 :(得分:0)

您需要添加注释:@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")@JsonSerialize(using = DateTimeSerializer.class),如:

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonSerialize(using = DateTimeSerializer.class)
@Column(name = "bill_date")
private DateTime billDate;

// Getter and setter...