2013-02-27 91 views
0

Hibernate映射:沒有Hibernate映射入門級數據

我有以下兩類:

class Employee{ 
    int empId; 
    string name; 
    float salary; 
    Department dept; 
    // ... getters and setters.... 
} 


class Department{ 
    int deptId; 
    string deptName; 
    // ....and other details, getter and setters.. 
} 

現在我有表,員工只喜歡

table Employee(EmpId number, Name varchar, salary number,deptId number) 

沒有必要請保留Department的表格,因爲我只對存儲具有dept id的員工數據感興趣。

我找hibernate mapping許多到一,一到一,一到多,但所有存儲dept data單獨table需要。

我的問題:有沒有辦法只保留簡單的映射文件,我可以從Department classEmployee mapping訪問deptId

感謝

回答

0

不要讓現場的任何關聯 DEPTID,治療 DEPTID簡單屬性,像你這樣做是爲了工資

0

我認爲,如果你只需要存儲deptId,你不需要創建一個Departement類。 因爲在Hibernate中,實體類是從Table in Database中表示的。

而且可能是,你可以練習它,你嘗試刪除從Department類@Entity註解, @Entity類,是標註爲類成爲表數據庫。

對不起,我的英語不好。

0
Can I access dept Id from `Department` class in Employee mapping? 

設想一下,一旦你停止程序並重新開始,你有一個department id與你,你會向誰請求data of that Class ??

不,您必須爲mapping做那個,並且必須在DB中創建相應的列。

0

只需將其映射爲整數即可。您仍然可以將其映射到EMPLOYEE表上的「DEPT_ID」列:

class Employee{ 
    ... 

    @Column(name = "DEPT_ID") 
    Integer deptId; 

    ... getters and setters.... 
}