2014-12-02 76 views
0

實體正確的DDL我有兩個實體的人&狀態與多對一關係問題創建使用的EclipseLink

@Entity 
@Table(name = "PERSON") 
public class Person implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id; 

    @Column(name = "FIRST_NAME") 
    private String firstName; 

    @Column(name = "LAST_NAME") 
    private String lastName; 

    @JoinColumn(name = "STATUS_ID", referencedColumnName = "ID") 
    @ManyToOne(cascade = CascadeType.ALL) 
    private Status status; 

    // setters & getters 
} 

@Entity 
@Table(name = "STATUS") 
public class Status implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID") 
    private Integer id; 

    @Column(name = "ABBREVIATION") 
    private String abbreviation; 

    @Column(name = "TITLE") 
    private String title; 

    @Basic(fetch=FetchType.LAZY) 
    @OneToMany(mappedBy = "status") 
    private Collection<Person> personCollection; 

    // setters & getters  
} 

當我嘗試自動生成,這些實體的我得到的數據庫表以下DDL:

CREATE TABLE `person` (
`ID` INT(11) NOT NULL AUTO_INCREMENT, 
`FIRST_NAME` VARCHAR(255) NULL DEFAULT NULL, 
`LAST_NAME` VARCHAR(255) NULL DEFAULT NULL, 
`NICK_NAME` VARCHAR(255) NULL DEFAULT NULL, 
`STATUS_ID` INT(11) NULL DEFAULT NULL, 
PRIMARY KEY (`ID`), 
INDEX `FK_PERSON_STATUS_ID` (`STATUS_ID`), 
CONSTRAINT `FK_PERSON_STATUS_ID` FOREIGN KEY (`STATUS_ID`) REFERENCES `status` (`ID`) 
) 

CREATE TABLE `status` (
`ID` INT(11) NOT NULL AUTO_INCREMENT, 
`ABBREVIATION` VARCHAR(255) NULL DEFAULT NULL, 
`PERSONCOLLECTION` LONGBLOB NULL, 
`TITLE` VARCHAR(255) NULL DEFAULT NULL, 
PRIMARY KEY (`ID`) 

顯然有些問題,因爲我的status表中不需要有PERSONCOLLECTION LONGBLOB字段。 我是否在做錯了映射?

使用:Netbeans的8,的EclipseLink,JPA 2.1和MySQL 5.6

回答

1

嘗試刪除@basic註釋也可以使用 「列表」,而不是 「收藏」

+1

去除@basic的伎倆。因爲OneToMany關係在默認情況下被延遲加載,所以我應該使用'@OneToMany(mappedBy =「status」,fetch = FetchType.LAZY)'而不是'@OneToMany(mappedBy =「status」)'''。 – 2014-12-02 02:29:18