0

我正在使用 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_。身份证=?”; 预期的“标识符”;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=? [42001-195] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.195.jar:1.4.195] 在 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:

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

预先感谢,罗曼。

4

1 回答 1

2

错误的重要部分是:

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

SQL 在某些时候需要一个标识符,但找不到它。关键字from是列名应该在的位置:这就是问题所在。

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

@Column(name = "date_from")
@Convert(converter = LocalDateAttributeConverter::class) val from: LocalDate,
于 2018-01-18T21:34:11.440 回答