使用Kotlin和SpringBoot坚持LocalDate

时间:2018-01-18 17:13:19

标签: kotlin spring-data-jpa localdate spring-boot-starter

我正在尝试使用SpringBoot和JPA的Kotlin。我尝试持久化LocalDate但有错误。以下是我的实体的代码:

@Entity
@Table(name = "season")
data class Season(val name: String,
              @Convert(converter = LocalDateAttributeConverter::class) val from: LocalDate,
              @Convert(converter = LocalDateAttributeConverter::class) val to: LocalDate,
              @Enumerated(EnumType.STRING) val status: Status,
              @Id @GeneratedValue val id: Int = -1)

enum class Status {
    CURRENT, CLOSED
}

转换器:

@Converter(autoApply = true)
class LocalDateAttributeConverter : AttributeConverter<LocalDate, Date> {

    override fun convertToDatabaseColumn(locDate: LocalDate?): Date? {
        return if (locDate == null) null else Date.valueOf(locDate)
    }

    override fun convertToEntityAttribute(sqlDate: Date?): LocalDate? {
        return sqlDate?.toLocalDate()
    }
}

当我尝试创建实体时:

val season = seasonRepository.save(Season("2017-2018",
        LocalDate.of(2017, Month.SEPTEMBER, 1),
        LocalDate.of(2018, Month.JULY, 31),
        Status.CURRENT))

错误堆栈如下:

  

引起:org.h2.jdbc.JdbcSQLException:SQL语句中的语法错误   “SELECT SEASON0_.ID AS ID1_6_0_,SEASON0_.FROM [*] AS FROM2_6_0_,   SEASON0_.NAME AS NAME3_6_0_,SEASON0_.STATUS AS STATUS4_6_0_ FROM   SEASON SEASON0_ WHERE SEASON0_.ID =? “;期望的”标识符“; SQL   声明:选择season0_.id为id1_6_0_,season0_.from as   from2_6_0_,season0_.name为name3_6_0_,season0_.status as   status4_6_0_来自赛季season0_其中season0_.id =? [42001-195]在   org.h2.message.DbException.getJdbcSQLException(DbException.java:345)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.message.DbException.getSyntaxError(DbException.java:205)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readColumnIdentifier(Parser.java:3241)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readTermObjectDot(Parser.java:2737)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readTerm(Parser.java:2863)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readFactor(Parser.java:2384)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readSum(Parser.java:2371)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readConcat(Parser.java:2341)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readCondition(Parser.java:2172)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readAnd(Parser.java:2144)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.readExpression(Parser.java:2136)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parseSelectSimpleSelectPart(Parser.java:2047)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parseSelectSimple(Parser.java:2079)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parseSelectSub(Parser.java:1934)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parseSelectUnion(Parser.java:1749)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parseSelect(Parser.java:1737)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parsePrepared(Parser.java:448)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.parse(Parser.java:320)〜[h2-1.4.195.jar:1.4.195]     在org.h2.command.Parser.parse(Parser.java:292)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.command.Parser.prepareCommand(Parser.java:257)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.engine.Session.prepareLocal(Session.java:573)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.engine.Session.prepareCommand(Session.java:514)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.jdbc.JdbcPreparedStatement。(JdbcPreparedStatement.java:73)   〜[h2-1.4.195.jar:1.4.195] at   org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)   〜[h2-1.4.195.jar:1.4.195] at   sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)   〜[na:1.8.0_151] at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_151] at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_151] at java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_151] at   org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)   〜[tomcat-jdbc-8.5.15.jar:na] at   org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)   〜[tomcat-jdbc-8.5.15.jar:na] at   org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:75)   〜[tomcat-jdbc-8.5.15.jar:na] at   org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)   〜[tomcat-jdbc-8.5.15.jar:na] at   org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)   〜[tomcat-jdbc-8.5.15.jar:na] at   com.sun.proxy。$ Proxy66.prepareStatement(未知来源)〜[na:na] at   org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ 5.doPrepare(StatementPreparerImpl.java:146)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at   org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] ... 67个常见帧   省略

有没有人遇到同样的问题?

事先谢谢, 罗曼。

1 个答案:

答案 0 :(得分:2)

错误的重要部分是:

  

预期的“标识符”; SQL语句:选择season0_.id为id1_6_0_,season0_.from为from2_6_0_,(... snip ...)

SQL在某个时刻需要一个标识符,但无法找到它。关键字from是列名称的位置:这就是问题所在。

您可以重命名from属性,或者告诉JPA您要在SQL中将其称为其他内容,如下所示:

@Column(name = "date_from")
@Convert(converter = LocalDateAttributeConverter::class) val from: LocalDate,