2011-08-07 36 views
1

我试图在二维集合中存储数据表。 每当我:在Play中创建一个二维数组!框架

@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(); 
    } 

} 

回答

2

据我所知,@OneToMany只适用于实体列表。你正在做一个List of 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); 
+0

我想什么你说的和我越来越:发生 JPA错误 一个JPA错误(无法建立的EntityManagerFactory):无法实例测试objectmodels.Row – zmahir

+0

@zmahir你可以发布你使用的代码吗? –

+0

最初的帖子是用代码编辑的。 – zmahir