2011-08-07 34 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