简体   繁体   English

玩! 框架bootstrap(Fixtures.loadModels(“ initial-data.yaml)),在模型的setter中注入“ random”字符串值

[英]Play! framework bootstrap(Fixtures.loadModels(“initial-data.yaml)), injecting ”random" String value in model's setter

I have a model class with a persisted DateTime field that is only interacted via getters/setters for Transient properties, String date; 我有一个带有持久化DateTime字段的模型类,该类仅通过用于临时属性( String date; getter / setter进行交互String date; and String time; String time; . The do some very specific formatting to create the DateTime object that will be persisted or retrieved when need be. 执行一些非常特定的格式来创建DateTime对象,该对象将在需要时被保留或检索。

The problem is that when my model's loaded from the yaml file, the setter for the time field receives a String value that doesn't correspond at all to anything in my project/code. 问题是,当从yaml文件加载模型时, time字段的setter接收到的String值与我的项目/代码中的任何内容都不对应。

Here's the class with only relevant members: 这是只有相关成员的课程:

package models;

import javax.persistence.*;
import org.hibernate.annotations.*;
import org.joda.time.*;
import org.joda.time.format.*;

import play.db.jpa.*;

@javax.persistence.Entity
public class Booking extends Model {

  @Column
  @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
  public DateTime datetime;
  public Integer duration;
  @Transient
  public String date;
  @Transient
  public String time;    

  //default constructor called by play's model loader that sets default values that are required for the getters and setters to work.
  public Booking() {
    DateTimeFormatter fmt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
    this.datetime = fmt.parseDateTime("ISO8601:1970-01-01T00:00:00+0200");
    //this.datetime = fmt.parseDateTime(this.date+"T"+this.time);
  }

  public void setDate(String dateStr) {    
    this.date = dateStr;
    if (dateStr.contains("ISO")) {
      DateTimeFormatter dt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
      DateTime tmp = dt.parseDateTime(dateStr);
      this.datetime = toDateTime(tmp.toString("yyyy-MM-dd"), getTime());
    } else {
      this.datetime = toDateTime(dateStr, getTime());
    }

  }

  public void setTime(String timeStr) {
    this.time = timeStr; //timeStr = "780" for some reason?!
    if (timeStr.contains("ISO")) {
      DateTimeFormatter dt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
      DateTime tmp = dt.parseDateTime(timeStr);
      this.datetime = toDateTime(getDate(), tmp.toString("HH:mm"));
    }
    this.datetime = toDateTime(getDate(), timeStr);
  }

  public String getDate() {
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
    return this.datetime.toString(format);
  }

  public String getTime() {
    DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm");
    return this.datetime.toString(format);
  }

  private DateTime toDateTime(String dateStr, String timeStr) {
    DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinute();
    DateTime dt = fmt.parseDateTime(dateStr + "T" + timeStr);
    return dt;
  }

When I run through the debugger, the timeStr parameter that setTime receives when it's first called is "780" . 当我运行调试器时,setTime首次调用时收到的timeStr参数是"780" There is no such value in my yaml file as the model is injected like this: 我的yaml文件中没有这样的值,因为模型是这样注入的:

Booking(bobBooking):
  date: 2011-09-16
  time: 13:00
  duration: 30
  headcount: 10
  room: b
  user: bob
  description: Bob's Booking.

The additional fields are omitted. 省略其他字段。

Try using quotes for time value in yaml file. 尝试对Yaml文件中的时间值使用引号。 There could be some issue in parsing colon fields using SnakeYAML parser (which is the default in Play) 使用SnakeYAML解析器解析冒号字段时可能会出现问题(这是Play中的默认设置)

YAML 1.1 defines 13:00 as a sexagesimal value (which is not what you expect) YAML 1.1将13:00定义为十六进制值(这不是您期望的值)

http://yaml.org/type/int.html http://yaml.org/type/int.html

Use single or double quotes to specify a string value. 使用单引号或双引号指定字符串值。 ('13:00', "13:00") (“ 13:00”,“ 13:00”)

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

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