我正在尝试在二维集合中存储数据表。 每当我:
@OneToMany
public List<List<Cell>> cells;
我收到了JPA错误:
JPA错误 发生了JPA错误(无法构建EntityManagerFactory):使用@OneToMany或@ManyToMany定位未映射的类: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();
}
}
答案 0 :(得分:2)
据我所知,@OneToMany
仅适用于实体列表。您正在执行列表列表,该列表不是实体,因此失败。
尝试将模型更改为:
表&gt;行&gt;细胞
所有这些都是通过@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);