SQL persist() 中的语法异常

SQL Grammar Exception in persist()

我正在尝试保留一个实体,但我收到一个错误:

could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

正在执行

public void createTransaction(PositionTransaction account){
        em.persist(account);
        em.refresh(account);
    }

所有 account 字段都已填写(id 除外 - 它是一个 IDENTITY)

这是我的 PositionTransaction 映射:

@Entity
@Table(name = "POSITION_PROJ_TRANSACTION")
public class PositionTransaction {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID", columnDefinition = "NUMERIC(15, 0)",unique = true,nullable = false)
private Long id;

@Column(name = "VALUE_DATE")
private LocalDate valueDate;

@Column(name = "AMOUNT")
private BigDecimal balance;

@Column(name = "COMMENT")
private String comment;

@Column(name = "STATUS")
private Integer status;

@ManyToOne
@JoinColumn(name = "PARENT_ID")
private FaAccount positionAccount;
public FaAccount getPositionAccount() { return positionAccount; }
public void setPositionAccount(FaAccount positionAccount) {
    this.positionAccount = positionAccount;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public LocalDate getValueDate() {
    return valueDate;
}

public void setValueDate(LocalDate valueDate) {
    this.valueDate = valueDate;
}

public BigDecimal getBalance() {
    return balance;
}

public void setBalance(BigDecimal balance) {
    this.balance = balance;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

@Override
public String toString() {
    return "PositionTransaction{" +
            "id=" + id +
            ", valueDate=" + valueDate +
            ", balance=" + balance +
            ", comment='" + comment + '\'' +
            ", status=" + status +
            ", positionAccount=" + positionAccount +
            '}';
}

@Override
public int hashCode() {
    return id != null ? id.hashCode() : 0;
}

}

跟踪:

17:55:21.210 [http-nio-8081-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper -  [] SQL Error: 210, SQLState: S0001
17:55:21.210 [http-nio-8081-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper -  [] Conversion failed when converting datetime from binary/varbinary string.
17:55:21.215 [http-nio-8081-exec-1] ERROR r.c.i.a.a.a.PositionEndpoint -  [] X: 
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at 

根据您的意见,我修改了代码,但这里有另一个例外:

18:41:01.389 [JavaFX Application Thread] ERROR io.datafx.core.ExceptionHandler -  [] DataFX Exception Handler
io.datafx.controller.flow.FlowException: java.lang.reflect.InvocationTargetException
    at io.datafx.controller.flow.action.FlowMethodAction.handle(FlowMethodAction.java:53) ~[flow-8.0.jar:na]
    at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:231) [flow-8.0.jar:na]
    at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:163) [flow-8.0.jar:na]
    at io.datafx.controller.flow.FlowHandler.handleActionWithExceptionHandler(FlowHandler.java:338) [flow-8.0.jar:na]
    at io.datafx.controller.flow.FlowHandler.lambda$attachEventHandler(FlowHandler.java:304) [flow-8.0.jar:na]
    at io.datafx.controller.flow.FlowHandler$$Lambda/186245904.handle(Unknown Source) [flow-8.0.jar:na]
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) [jfxrt.jar:na]
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) [jfxrt.jar:na]
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) [jfxrt.jar:na]
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) [jfxrt.jar:na]
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) [jfxrt.jar:na]
    at javafx.event.Event.fireEvent(Event.java:198) [jfxrt.jar:na]
    at javafx.scene.Node.fireEvent(Node.java:8390) [jfxrt.jar:na]
    at javafx.scene.control.Button.fire(Button.java:185) [jfxrt.jar:na]
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) [jfxrt.jar:na]
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96) [jfxrt.jar:na]
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89) [jfxrt.jar:na]
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) [jfxrt.jar:na]
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) [jfxrt.jar:na]
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) [jfxrt.jar:na]
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) [jfxrt.jar:na]
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [jfxrt.jar:na]
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [jfxrt.jar:na]
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) [jfxrt.jar:na]
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) [jfxrt.jar:na]
    at javafx.event.Event.fireEvent(Event.java:198) [jfxrt.jar:na]
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3758) [jfxrt.jar:na]
    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3486) [jfxrt.jar:na]
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) [jfxrt.jar:na]
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495) [jfxrt.jar:na]
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350) [jfxrt.jar:na]
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275) [jfxrt.jar:na]
    at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_45]
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent0(GlassViewEventHandler.java:385) [jfxrt.jar:na]
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda4/84707264.get(Unknown Source) [jfxrt.jar:na]
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404) [jfxrt.jar:na]
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384) [jfxrt.jar:na]
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555) [jfxrt.jar:na]
    at com.sun.glass.ui.View.notifyMouse(View.java:927) [jfxrt.jar:na]
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) [jfxrt.jar:na]
    at com.sun.glass.ui.win.WinApplication.lambda$null5(WinApplication.java:101) [jfxrt.jar:na]
    at com.sun.glass.ui.win.WinApplication$$Lambda/1596000437.run(Unknown Source) [jfxrt.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at io.datafx.controller.flow.action.FlowMethodAction.handle(FlowMethodAction.java:51) ~[flow-8.0.jar:na]
    ... 66 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at io.datafx.controller.injection.InjectionHandler.invoke(InjectionHandler.java:87) ~[injection-8.0.jar:na]
    at ru.com.isbank.adc.adc_front_office_app.loro_nostro.LoroNostroModel_$$_jvstd5e_6.getAddDate(LoroNostroModel_$$_jvstd5e_6.java) ~[classes/:na]
    at ru.com.isbank.adc.adc_front_office_app.loro_nostro.LoroNostroTrController.addTransaction(LoroNostroTrController.java:151) ~[classes/:na]
    ... 71 common frames omitted
Caused by: java.lang.NullPointerException: null
    at ru.com.isbank.adc.adc_front_office_app.loro_nostro.LoroNostroModel.getAddDate(LoroNostroModel.java:195) ~[classes/:na]
    ... 78 common frames omitted

有什么问题吗?有任何想法吗?谢谢

首先,回答您上面的评论是的,您应该更改 LocalDate,因为 jdbc 目前不支持(如果我错了,请告诉我)。使用 java.sql.Date 代替它是最简单和更快的解决方案

另一个解决方案是:

如堆栈跟踪所述,问题出在 LocalDate 中,如果您需要使用此类型,我会将其作为对象内部的 属性 但不是持久化。还有一个 属性,日期值为 java.sql.Date,类型为 java.sql.Date。给你一个例子,我会按如下方式(它可能无法完全编译,因为我现在不在我的电脑上):

  @Column(name = "VALUE_DATE")
      private  java.sql.Date persistendDate;

      private LocalDate valueDate;

      public void setValueDate(LocalDate valueDate) {
            this.valueDate = valueDate;
            this.persistendDate = java.sql.Date.valueOf(valueDate);
        }