关联映射:
1.关于n:1 使用emp和dept进行举例
单项多对一:
emp:
entity:+private Dept dept;
hbm.xml:+<many-to-one name="dept" column="deptno" class="com.entity.Dept" cascade="save-update" />
dept无需修改
双向一对多:
emp:
entity:+private Dept dept;
hbm.xml:+<many-to-one name="dept" column="deptno" class="com.entity.Dept" cascade="save-update" />
dept:
entity:+private List<Emp> emps;
hbm.xml:+<bag name="emps" inverse="true" cascade="all">
<key>
<column name="DEPTNO" precision="2" scale="0" />
</key>
<one-to-many class="com.entity.Emp" />
</bag>
关于inverse和cascade的设置
inverse:反转 默认inverse="false"表示主动方 一般将多方设置为主动方
cascade:级联 none save-update delete all 一般对一方设置级联
2.多对多:使用Student表和Course表 中间需要一个StuCourse学生课程表
单向多对多:
Student:
entity:+private List<Course> courses = new ArrayList<Course>();
hbm.xml:
<bag name="courses" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="STUNO" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany.entity.Course" >
<column name="COURSEID" precision="4" scale="0" />
</many-to-many>
</bag>
Course:无需修改
注意事项:关系表中只保存两列外键,设置inverse="false",在设置级联时需要设置cascade="save-update"
双向多对多:
Student:
entity:+private List<Course> courses = new ArrayList<Course>();
hbm.xml:
<bag name="courses" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="STUNO" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany.entity.Course" >
<column name="COURSEID" precision="4" scale="0" />
</many-to-many>
</bag>
Course:
entity:+private List<Student> students = new ArrayList<Student>();
hbm.xml:
<bag name="students" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="COURSEID" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany02.entity.Student" >
<column name="STUNO" precision="4" scale="0" />
</many-to-many>
</bag>
注意事项:关系表中只保存两列外键,设置inverse="false",在设置级联时需要设置cascade="save-update"
3.一对一:Employee员工表和Passport员工信息表
1.关于n:1 使用emp和dept进行举例
单项多对一:
emp:
entity:+private Dept dept;
hbm.xml:+<many-to-one name="dept" column="deptno" class="com.entity.Dept" cascade="save-update" />
dept无需修改
双向一对多:
emp:
entity:+private Dept dept;
hbm.xml:+<many-to-one name="dept" column="deptno" class="com.entity.Dept" cascade="save-update" />
dept:
entity:+private List<Emp> emps;
hbm.xml:+<bag name="emps" inverse="true" cascade="all">
<key>
<column name="DEPTNO" precision="2" scale="0" />
</key>
<one-to-many class="com.entity.Emp" />
</bag>
关于inverse和cascade的设置
inverse:反转 默认inverse="false"表示主动方 一般将多方设置为主动方
cascade:级联 none save-update delete all 一般对一方设置级联
2.多对多:使用Student表和Course表 中间需要一个StuCourse学生课程表
单向多对多:
Student:
entity:+private List<Course> courses = new ArrayList<Course>();
hbm.xml:
<bag name="courses" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="STUNO" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany.entity.Course" >
<column name="COURSEID" precision="4" scale="0" />
</many-to-many>
</bag>
Course:无需修改
注意事项:关系表中只保存两列外键,设置inverse="false",在设置级联时需要设置cascade="save-update"
双向多对多:
Student:
entity:+private List<Course> courses = new ArrayList<Course>();
hbm.xml:
<bag name="courses" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="STUNO" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany.entity.Course" >
<column name="COURSEID" precision="4" scale="0" />
</many-to-many>
</bag>
Course:
entity:+private List<Student> students = new ArrayList<Student>();
hbm.xml:
<bag name="students" table="STUCOURSE" inverse="false" cascade="save-update">
<key>
<column name="COURSEID" precision="4" scale="0" />
</key>
<many-to-many class="com.manytomany02.entity.Student" >
<column name="STUNO" precision="4" scale="0" />
</many-to-many>
</bag>
注意事项:关系表中只保存两列外键,设置inverse="false",在设置级联时需要设置cascade="save-update"
3.一对一:Employee员工表和Passport员工信息表
主键关联:
Employee:
entity:+private Passport passport;
hbm.xml:
<one-to-one name="passport" class="com.entity.Passport" cascade="all" />
Passport:
entity:+private Employee employee;
hbm.xml:
<generator class="foreign" >
<param name="property">employee</param>
</generator>
<one-to-one name="employee" class="com.entity.Employee" />
注意事项:主键生成策略选择foreign
唯一外键:
Employee:
entity:+private Passport passport;
hbm.xml:
<one-to-one name="passport" class="com.entity.Passport" property-ref="employee" cascade="all" />
Passport:
entity:+private Employee employee;
hbm.xml:
<many-to-one name="employee" column="empno" class="com.entity.Employee" unique="true">
Employee:
entity:+private Passport passport;
hbm.xml:
<one-to-one name="passport" class="com.entity.Passport" cascade="all" />
Passport:
entity:+private Employee employee;
hbm.xml:
<generator class="foreign" >
<param name="property">employee</param>
</generator>
<one-to-one name="employee" class="com.entity.Employee" />
注意事项:主键生成策略选择foreign
唯一外键:
Employee:
entity:+private Passport passport;
hbm.xml:
<one-to-one name="passport" class="com.entity.Passport" property-ref="employee" cascade="all" />
Passport:
entity:+private Employee employee;
hbm.xml:
<many-to-one name="employee" column="empno" class="com.entity.Employee" unique="true">