Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 游戏中的抽象模型界面!ORM,initial-data.yml是如何加载的?_Java_Design Patterns_Orm_Persistence_Playframework - Fatal编程技术网

Java 游戏中的抽象模型界面!ORM,initial-data.yml是如何加载的?

Java 游戏中的抽象模型界面!ORM,initial-data.yml是如何加载的?,java,design-patterns,orm,persistence,playframework,Java,Design Patterns,Orm,Persistence,Playframework,我有一个叫Booking的模型,它有一个持久的DateTime字段。但是,我不想直接与这个字段交互,而是通过两个临时字符串字段date和time进行交互。问题是,我不知道play如何/何时将数据加载到字段中-它似乎没有使用我提供的构造函数,因为DateTime字段总是空的 public class Booking extends Model { @Column @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime"

我有一个叫Booking的模型,它有一个持久的DateTime字段。但是,我不想直接与这个字段交互,而是通过两个临时字符串字段date和time进行交互。问题是,我不知道play如何/何时将数据加载到字段中-它似乎没有使用我提供的构造函数,因为DateTime字段总是空的

public class Booking extends Model {

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

  public Booking(String date, String time, Integer duration) {
    this.datetime = toDateTime(date, time);
    this.duration = duration;
  }

  public void setDate(String dateStr) {
    this.date = dateStr;
    this.datetime = toDateTime(dateStr, this.time);
  }

  public void setTime(String timeStr) {
    this.time = timeStr;
    this.datetime = toDateTime(this.date, timeStr);
  }

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

  public String getTime() {
    DateTimeFormatter format = DateTimeFormat.forPattern("kk:mm");
    return this.datetime.toString(format);//NullPointerException here!
  }
以下是toDateTime方法:

  private DateTime toDateTime(String date, String time){
    DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinute();
    DateTime dt = fmt.parseDateTime(date+"T"+time);

    return dt;
  }

play使用的JPA使用默认的空构造函数来初始化类。在您的例子中,Play框架(我猜)创建了Booking()构造函数。然后,JPA使用getter和setter设置实体的属性

也许您可以使用JPA中的@PostLoad注释。这将导致在将持久数据加载到实体中后调用带注释的方法

更新:我提到了@PostLoad,但也许@PrePersist是一个更好的选项,用于检查DateTime字段是否为空,如果是这种情况,可以使用默认值设置它。这样:

@PrePersist
public void prePersist()
{
    if(this.dateTime==null)
    {
        this.dateTime = toDateTime(this.date, this.time);
    }
}

在上面的代码中,我假设DateTime是joda DateTime。我认为JPA/Hibernate不支持这种数据类型的持久性。支持的是时间戳,日历作为JDK的一部分提供


您必须在Hibernate中定义新的用户类型才能使用DateTime。选中此项

当JPA加载对象时,您似乎可以定义一个默认构造函数,该构造函数不需要任何参数来设置对象。像这样:

public Booking() {
  DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinute();
  this.datetime = fmt.parseDateTime("1970-01-01T00:00");
}
现在唯一的问题是,它使用了我在从数据库检索时定义的相同设置器:*原因:java.lang.IllegalArgumentException:无效格式:“ISO8601:2011-08-25T02:00:00+0200…”
*

谢谢
@PostLoad
有效,但
@PrePersist
无效。此外,我还必须在方法调用中指定默认值:
this.datetime=toDateTime(“1970-01-01”,“00:00”)等等,现在出现了一个问题,即值将永远绑定到其默认值,而不是由
时间
日期
字段的getter和setter设置。我不理解您的字段为什么为空。如果使用构造函数创建新预订,datetime成员变量永远不能为null。然后,持久化该对象将导致在数据库中存储一个值。请检查您的数据库是否实际存储了该值。如果不是这样,我想Joda DateTime可能有问题。我不知道Joda DateTime。数据是通过play引导加载的,使用
Fixtures.loadModels(“initial data.yml”)。使用调试器,既不会调用我的构造函数,也不会调用setter。我将实现一个测试,看看这种行为是否仅与引导隔离。fixture也使用默认构造函数,可能它不直接调用和setXXX方法。因此我想@PrePersist应该确保dateTime字段设置正确。您能提供一个创建预订的.yml文件的示例吗?我使用的是joda time hibernate,它确实支持它。我只是省略了代码示例中的注释,我只需要知道JPA加载实体的顺序,以确保它所依赖的另一个字段不为空。