2012-04-03 97 views
0

我創建了一個播放應用程序,並有一些模型。其中一個包含一個對象設置,但JPA不能創建表...JPA @ElementCollection播放框架錯誤

package models; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.Map; 
import java.util.Set; 

import javax.persistence.ElementCollection; 
import javax.persistence.Entity; 
import javax.persistence.Table; 

import play.db.jpa.Model; 

@Entity 
public class Receipt extends Model { 

    @ElementCollection 
    Set<Serving> servings; 
    DiningTable table; 

    public Receipt(Set<Serving> servings, DiningTable table) { 
     super(); 
     this.servings = servings; 
     this.table = table; 
    } 

} 

和錯誤是

 
21:20:47,323 INFO ~ Listening for HTTP on port 9000 (Waiting a first request to start) ... 
21:20:51,935 INFO ~ Connected to jdbc:mysql://localhost/rms_?>useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci 
21:20:52,827 ERROR ~ Unsuccessful: create table Receipt (id bigint not null auto_increment, >table tinyblob, primary key (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 
21:20:52,827 ERROR ~ You have an error in your SQL syntax; check the manual that >corresponds to your MySQL server version for the right syntax to use near 'table tinyblob, >primary key (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 1 
21:20:53,183 ERROR ~ Unsuccessful: alter table Receipt_Serving add index FKB18778C559F3AF16 >(Receipt_id), add constraint FKB18778C559F3AF16 foreign key (Receipt_id) references Receipt >(id) 
21:20:53,183 ERROR ~ Can't create table 'rms_.#sql-731_f2' (errno: 150) 
21:20:53,414 INFO ~ Application 'rms' is now started ! 

我怎樣才能解決這個問題?

回答

1

table是保留關鍵字。嘗試使用不同columname通過改變字段名或通過添加:

@Column(name="diningTable") 

而且,我懷疑你想DiningTable成爲一個實體,而不是存儲爲字節碼(TINYBLOB)。如果是的話,你應該添加一些關係註釋。

+0

哪些關係註釋?像manytoone或manytomany? – 2012-04-03 19:05:49

+0

由於您沒有收據上的表格集合,它將是\ @ManyToOne。你確定你想在你的服務中使用\ @ElementCollection嗎?它是\ @Embedable,你不想單獨查詢?也許你應該考慮\ @OneToMany? – barsju 2012-04-03 19:16:00

+1

http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection – barsju 2012-04-03 19:17:02