Hibernate 笔记8 关系映射1(多对一,一对多)
1 多对一关系(many to one)
多对一关系就是多的一方(员工类Emp)拥有一的一方(部门类Dept)的引用,一个员工必然属于一个部门,所以员工表中要有部门的信息(部门编号did),这就是对部门类Dept的引用。
表关系与类关系如下所示,类Emp中的dept对应表emp中的did。多对一关系中,外键往往在多的一方。
箭头指向被引用处
映射文件:
<hibernate-mapping >
<class name="com.pk.Test.po.Dept" table="dept"> //Dept类的映射
<id name="did" column="did">
<generator class="native"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="daddress" column="daddress"></property>
</class>
<class name="com.pk.Test.po.Emp" table="emp"> // Emp类的映射
<id name="eid" column="eid">
<generator class="native"></generator>
</id>
<property name="ename" column="ename"></property>
<property name="eage" column="eage"></property>
<property name="esal" column="esal"></property>
<many-to-one name="dept"> //对dept的配置,多对一,生成外键did,对应Emp的dept.
<column name="did"></column>
</many-to-one>
</class>
</hibernate-mapping>
测试:
public void TestMany2one() throws Exception{
Configuration config=new Configuration().configure();
SchemaExport export= new SchemaExport(config);
export.create(true, true);
}
自动生成的sql语句
alter table emp drop foreign key FK188C8F7B42ADD
drop table if exists dept
drop table if exists emp
create table dept (did integer not null auto_increment, dname varchar(255), daddress varchar(255), primary key (did))
create table emp (eid integer not null auto_increment, ename varchar(255), eage integer, esal float, did integer, primary key (eid))
alter table emp add index FK188C8F7B42ADD (did), add constraint FK188C8F7B42ADD foreign key (did) references dept (did)
2 一对多关系映射(one to many)
一对多关系就是,一的一方(部门类Dept)有多的一方(员工类Emp)的引用,一个部门必然有员工,所以在部门表中有员工信息,这就是对员工类Emp的引用。
表关系与类关系如下所示,类Dept中的emps对应Emp的did。一对多关系中,外键往往在多的一方。
映射文件:
<hibernate-mapping >
<class name="com.pk.Test.po.Dept" table="dept">
<id name="did" column="did"> //Dept类映射
<generator class="native"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="daddress" column="daddress"></property>
<set name="emps"> // 类中集合Set用<set>映射,name中填写Set类对象,emps与did成对应关系
<key column="did"/> // <key>表示 产生外键 did
<one-to-many class="com.pk.Test.po.Emp"/> //在哪个类的映射文件,哪个类就是one, class中写入many的类路径
</set>
</class>
<class name="com.pk.Test.po.Emp" table="emp"> //Emp类映射
<id name="eid" column="eid">
<generator class="native"></generator>
</id>
<property name="ename" column="ename"></property>
<property name="eage" column="eage"></property>
<property name="esal" column="esal"></property>
</class>
</hibernate-mapping>
测试
public void Test2() throws Exception{
Configuration config=new Configuration().configure();
SchemaExport export= new SchemaExport(config);
export.create(true, true);
}
结果:
alter table emp drop foreign key FK188C8F7B42ADD
drop table if exists dept
drop table if exists emp
create table dept (did integer not null auto_increment, dname varchar(255), daddress varchar(255), primary key (did))
create table emp (eid integer not null auto_increment, ename varchar(255), eage integer, esal float, did integer, primary key (eid))
alter table emp add index FK188C8F7B42ADD (did), add constraint FK188C8F7B42ADD foreign key (did) references dept (did)
3 一对多,多对一双向映射
此映射是前面两个映射的集合,部门类引用员工类,同时员工类引用部门类。
表关系与类关系如下所示,类Dept中的emps和Emp的dept 对应Emp的did。
映射:
<hibernate-mapping >
<class name="com.pk.Test.po.Dept" table="dept"> //Dept映射
<id name="did" column="did">
<generator class="native"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="daddress" column="daddress"></property>
<set name="emps">
<key column="did"/> //一对多关系映射
<one-to-many class="com.pk.Test.po.Emp"/>
</set>
</class>
<class name="com.pk.Test.po.Emp" table="emp"> //Emp映射
<id name="eid" column="eid">
<generator class="native"></generator>
</id>
<property name="ename" column="ename"></property>
<property name="eage" column="eage"></property>
<property name="esal" column="esal"></property>
<many-to-one name="dept">
<column name="did"></column> //多对一关系映射
</many-to-one>
</class>
</hibernate-mapping>
测试:
public void Test2() throws Exception{
Configuration config=new Configuration().configure();
SchemaExport export= new SchemaExport(config);
export.create(true, true);
}
结果:
alter table emp drop foreign key FK188C8F7B42ADD
drop table if exists dept
drop table if exists emp
create table dept (did integer not null auto_increment, dname varchar(255), daddress varchar(255), primary key (did))
create table emp (eid integer not null auto_increment, ename varchar(255), eage integer, esal float, did integer, primary key (eid))
alter table emp add index FK188C8F7B42ADD (did), add constraint FK188C8F7B42ADD foreign key (did) references dept (did)