【问题标题】:Change SQL to Criteria or HQL将 SQL 更改为 Criteria 或 HQL
【发布时间】:2018-09-17 08:41:56
【问题描述】:

更改此查询

SELECT 
    * 
FROM 
    Authors 
WHERE 
    born <= DATE_ADD(CURDATE(), interval -55 year) 

到条件查询。 这是我的错误尝试

 List<Book> bookList = session.createQuery("from Book where born <= DATE_SUB(current_date , INTERVAL 10 year)").list();

【问题讨论】:

  • 您能否发布堆栈跟踪以及有关您遇到的问题的更多信息?

标签: java mysql sql spring hibernate


【解决方案1】:

问题是 Spring 将请求作为 json 对象发送,然后尝试将它们转换为您传入的模型。在这种情况下,它是“Author”,它将出生定义为 Date,但正在发送的字符串是'不是要转换为日期的正确日期格式。

有两种不同的方法可以处理这个问题。

1) 创建一个将日期定义为字符串的作者 POJO,然后在指定日期格式的同时手动转换为作者实体

2) 创建一个 Date 解串器类,它将获取字符串并将其解串为指定字段的日期:

这是使用杰克逊库:

public class DateTimeStampDeserializer extends JsonDeserializer<Date> {

  private static final String DEFAULT = "MM/dd/yyyy";
  private static final String EXPANDED = "MM/dd/yyyy HH:mm:ss z";
  private static final String EXPANDED_WITH_TIMEZONE = "MMM d, yyyy HH:mm:ss z";
  private static final String EXPANDED_WITH_AM_PM = "MMM d, yyyy h:mm:ss a";

  private static final String FORMAT_1 = "yyyy-dd-MM";
  private static final String FORMAT_2 = "yyyy/dd/MM";
  private static final String FORMAT_3 = "yyyy.dd.MM";
  private static final String FORMAT_4 = "yyyyddMM";

  private static final String[] formats = new String[] {
      DEFAULT,
      EXPANDED,
      EXPANDED_WITH_TIMEZONE, 
      EXPANDED_WITH_AM_PM,
      FORMAT_1,
      FORMAT_2,
      FORMAT_3,
      FORMAT_4
  };

  private static final Integer[] styles = new Integer[] {
      SimpleDateFormat.LONG,
      SimpleDateFormat.FULL,
      SimpleDateFormat.MEDIUM,
      SimpleDateFormat.SHORT
  };

  @Override
  public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    String value = p.getText();

    Date date = null;
    //First try to see if the value can be parsed into a long
    try {
      date = new Date(Long.parseLong(value));
      return date;
    } catch (Exception ex) { }

    //Next iterate over the built in styles to see if it can be converted
    for (Integer style: styles) {
      date = formatDate(style, value);
      if (date != null) {
        return date;
      }
    }

    //Lastly iterate over the custom styles specified in format to see if it can be converted
    for (String fmt : formats) {
      date = formatDate(fmt, value);
      if (date != null) {
        return date;
      }
    }

    //Return null if date format can't be converted
    return null; 
  }

  /**
   * Convert a string value to a date object
   * @param format The format to use in reference to the source 
   * @param source the source to convert
   * @return Date object if the conversion was success; null otherwise
   */
  private static Date formatDate(String format, String source) {
    try {
      return new SimpleDateFormat(format).parse(source);
    } catch (Exception ex) {
      return null;
    }
  }

  /**
   * Convert a string value to a date object using SimpleDateFormats 
   * built in styles
   * @param style The style to use
   * @param source the source to convert
   * @return Date object if the conversion was success; null otherwise
   */
  private static Date formatDate(Integer style, String source) {
    try {
      return SimpleDateFormat.getDateInstance(style).parse(source);
    } catch (Exception ex) {
      return null;
    }
  }
}

然后在你的实体中使用它你很简单

@JsonDeserialize(using=DateTimeStampDeserializer.class)
@Column(name = "born")
@Temporal(TemporalType.DATE)
private java.util.Date born;

可以通过 maven 将 Jackson 库添加到您的项目中(如果使用 maven)

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.6</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.6</version>
</dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2011-10-27
    • 1970-01-01
    • 2016-03-08
    • 2014-10-08
    相关资源
    最近更新 更多