简体   繁体   English

Spring Boot-休眠-从SQL查询导入日期

[英]Spring Boot - Hibernate - Import Date from SQL Query

I am currently building a REST API with Spring Data (and Boot). 我目前正在使用Spring Data(和Boot)构建REST API。 I have a sql Dumb from an h2database, which is accessed by hibernate. 我有一个来自h2database的sql Dumb,可以通过hibernate访问。

My Application.yaml: 我的Application.yaml:

spring:
  profiles: "dev"
  datasource:
      data: classpath:/data_api.sql
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
      ddl-auto: create-drop

The sql query lools like this: sql查询如下:

Insert into TABLE (name, number, date) values ("James", 123, to_date('28-JUL-17','DD-MON-RR'))

Information.java looks like: Information.java看起来像:

@Entity  
@Table(name="TABLE") 
@Data 
public class Information {

    @Column(name="name")
    String name;

    @Column(name="number")
    Integer number;

    @Column(name="date")
    String date;

When I try to run the API I got the following exception: enter image description here 当我尝试运行API时,出现以下异常: 在此处输入图像描述

Caused by: org.h2.jdbc.JdbcSQLException: Function "TO_DATE": Invalid date format: " Tried to parse one of '[Jul, Feb, Apr, Jun, Aug, Mai, Nov, Jan, Dez, Okt, M├ñr, Sep]' but failed (may be an internal error?). Details: TO_DATE('16-MAR-17', 'DD-MON-RR') ^ , ^ <-- Parsing failed at this point"; 由以下原因引起:org.h2.jdbc.JdbcSQLException:函数“ TO_DATE”:无效的日期格式:“试图分析'[[Jul,Feb,Apr,Jun,Aug,Mai,Nov,Jan,Dez,Okt,M├ ñr,Sep]',但失败(可能是内部错误?)。详细信息:TO_DATE('16 -MAR-17','DD-MON-RR')^,^ <-解析失败。“ SQL statement SQL语句

My guess is that hibernate takes some locale where the months are configured in german - does anybody know how to change that? 我的猜测是,休眠状态需要使用德语配置月份的某些语言环境-有人知道如何更改吗? Another way would be to tell hibernate --> ignore the sql function to_date() and just read the field as string. 另一种方法是告诉休眠->忽略sql函数to_date()并仅将字段读取为字符串。 I tried something like writing @org.hibernate.annotations.Type(type =“text“) over the @column annotation - but it doesn't work :( 我尝试了类似在@column注释上编写@ org.hibernate.annotations.Type(type =“ text”)的方法-但它不起作用:(

EDIT: When changing the Monthname in the import queries to german --> like OCT --> OKT ; 编辑:将导入查询中的月份名称更改为德语->像OCT-> OKT时; DEC --> DEZ.. it works. DEC-> DEZ ..它有效。 So it seems that Hibernate is using my local language settings for mapping. 因此,似乎Hibernate正在使用我的本地语言设置进行映射。 Is there a way to change this to english? 有没有办法将其更改为英语?

If anybody else comes to this problem - I figuered it out. 如果有人遇到这个问题-我想通了。 Hibernate reads the Locate settings. Hibernate读取“定位”设置。 So with a simple code line in the Main Application this is done: 因此,在主应用程序中使用简单的代码行即可完成此操作:

Locale.setDefault(new Locale("en", "US"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM