简体   繁体   中英

Spring Boot - Hibernate - Import Date from SQL Query

I am currently building a REST API with Spring Data (and Boot). I have a sql Dumb from an h2database, which is accessed by hibernate.

My 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:

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

Information.java looks like:

@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

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"; SQL statement

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. I tried something like writing @org.hibernate.annotations.Type(type =“text“) over the @column annotation - but it doesn't work :(

EDIT: When changing the Monthname in the import queries to german --> like OCT --> OKT ; DEC --> DEZ.. it works. So it seems that Hibernate is using my local language settings for mapping. 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. So with a simple code line in the Main Application this is done:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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