hibernate 映射:
我有以下两个类(class):
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 ,例如
table Employee(EmpId number, Name varchar, salary number,deptId number)
不需要保留部门
表,因为我只对存储具有部门 ID 的员工数据感兴趣。
我正在寻找用于多对一、一对一、一对多的hibernate映射
,但所有这些都需要在单独的中存储
。部门数据
>表
我的问题:有没有办法只保留简单的映射文件,并且我可以在Employee映射
中从Department class
访问deptId
?
谢谢
最佳答案
如果使用 xml 映射 ( http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/components.html ),则需要将 Department 定义为 组件
;如果使用注释,则需要将 Department 定义为 @Embedded/@Embeddable
实体 (§ http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-property 中的 2.2.2.4 )
关于java - 无需 Hibernate 映射即可获取类数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115876/