Question

I'm working on a web-application that should use a model (Play Framwork 2.2.2) with an Integer array to hold one value for each day in a year. Something like this

@Entity
public class YearRecord extends Model {

  @Id
  public Integer year;  

  public Integer[] result = new Integer[366];
}

I would like to be able to save the model to a MySQL database, a calling application should be able to do something like this

YearRecord yr = new YearRecord();
yr.year = 2014;
// assign results here (some values may be left as null)
yr.result[0] = 7;
yr.result[1] = null;
// ...
yr.save();

Are saving arrays like this supported by the Play-framework, if so how may it be done and how should the MySQL table be defined? If not, what are the alternatives?

Was it helpful?

Solution

One option is to have a one-to-many mapping to a List of DayRecord where DayRecord is another entity containing the data you want to store. Something like:

@OneToMany
List<DayRecord> dayRecords;

...

@Entity
public class DayRecord extends Model {

  @Id
  private Integer id;

  private Integer day;  

  @ManyToOne
  @JoinColumn(name = "id")
  private YearRecord yearRecord;

  ...
}

Take a look at ebean's cascade documentation as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top