Jpa 在游戏中创建二维阵列!框架
我试图在二维集合中存储一个数据表。 每当我:Jpa 在游戏中创建二维阵列!框架,jpa,playframework,Jpa,Playframework,我试图在二维集合中存储一个数据表。 每当我: @OneToMany public List<List<Cell>> cells; @OneToMany 公共列表单元格; 我得到一个JPA错误: JPA错误 发生JPA错误(无法构建EntityManagerFactory):使用@OneToMany或@ManyToMany瞄准未映射的类:models.Table.cells[java.util.List] Cell是我创建的一个类,它基本上是一个字符串装饰器。有什么想法
@OneToMany
public List<List<Cell>> cells;
@OneToMany
公共列表单元格;
我得到一个JPA错误:
JPA错误
发生JPA错误(无法构建EntityManagerFactory):使用@OneToMany或@ManyToMany瞄准未映射的类:models.Table.cells[java.util.List]
Cell是我创建的一个类,它基本上是一个字符串装饰器。有什么想法吗?我只需要一个可以存储的二维矩阵
@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();
}
}
@实体公共类表扩展模型{
@独身癖
公共列表行;
公共表(){
this.rows=new ArrayList();
这是save();
}
}
@实体公共类行扩展模型{
@独身癖
公共列表单元格;
公共行(){
this.cells=new ArrayList();
这是save();
}
}
@实体公共类单元扩展模型{
公共字符串内容;
公共单元格(字符串内容){
this.content=内容;
这是save();
}
}
据我所知,@OneToMany
仅适用于实体列表。你正在做一个列表的列表,它不是一个实体,所以它失败了
尝试将模型更改为:
表>行>单元格
所有这些都是通过@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;
}
}
我完全按照你说的做了尝试,我得到了:JPA错误发生了JPA错误(无法构建EntityManagerFactory):无法实例化测试对象模型。Row我使用了你的解决方案,我注意到你在构造函数中去掉了.save(),但当我查看数据库单元格时,行被存储了,而表却没有@zmahir我认为最好不要在外面处理这个问题。用你的方式更新代码,我得到错误:org.hibernate.transientObject异常:对象引用未保存的临时实例-在刷新之前保存临时实例:models.Cell.row->models.row java.lang.IllegalStateException:org.hibernate.transientObject异常:对象引用未保存的临时实例-在刷新之前保存临时实例刷新:models.Cell.row->models.row
Row row = new Row();
row.save();
row.addCell("Content");
Table table = new Table();
table.save();
table.addRow(row);