在Play!中创建一个二维数组.框架



我试图在二维集合中存储数据表。每当我:

@OneToMany
public List<List<Cell>> cells;

我得到JPA错误:

JPA错误发生JPA错误(无法构建EntityManagerFactory):使用@OneToMany或@ manymany针对未映射的类:models.Table.cells[java.util.List]

Cell是我创建的一个类,它基本上是一个String装饰器。什么好主意吗?我只需要一个可以存储的二维矩阵。

@Entity public class Table extends Model {
    @OneToMany
    public List<Row> rows;
    public Table() {
        this.rows = new ArrayList<Row>();
        this.save();
    }
}
@Entity public class Row extends Model {
    @OneToMany
    public List<Cell> cells;
    public Row() {
        this.cells = new ArrayList<Cell>();
        this.save();
    }
}
@Entity public class Cell extends Model {
    public String content;
    public Cell(String content) {
        this.content = content;
        this.save();
    }
}

据我所知,@OneToMany仅适用于实体列表。你正在做List的List,它不是一个实体,所以它失败了。

尝试将模型更改为:

表>行>单元格

他们都通过@OneToMany,所以你可以有你的二维结构,但与实体。

编辑:

我认为你的模型声明不正确。试试这个:

@Entity public class Table extends Model {
    @OneToMany(mappedBy="table")
    public List<Row> rows;
    public Table() {
        this.rows = new ArrayList<Row>();
    }
    public Table addRow(Row r) {
        r.table = this;
        r.save();
        this.rows.add(r);      
        return this.save();
    }
}
@Entity public class Row extends Model {
    @OneToMany(mappedBy="row")
    public List<Cell> cells;
    @ManyToOne
    public Table table;
    public Row() {
        this.cells = new ArrayList<Cell>();
    }
    public Row addCell(String content) {
        Cell cell = new Cell(content);
        cell.row = this;
        cell.save();
        this.cells.add(cell);
        return this.save();
    }
}
@Entity public class Cell extends Model {
    @ManyToOne
    public Row row;       
    public String content;
    public Cell(String content) {
        this.content = content;
    }
}
创建:

Row row = new Row();
row.save();
row.addCell("Content");
Table table = new Table();
table.save();
table.addRow(row);

最新更新