标签:des style blog color strong 数据
数据关联映射分为:一对多、多对一,一对一,多对多。
cascade属性
• save-update
:在执行保存和修改是进行级联操作
• delete
:在执行删除时进行级联操作
• all
:在所有情况下进行级联操作
• none
:不进行级联操作(默认)
抓取策略
一对多、多对一
一对多、多对一持久化类设计 // User类 public class User { private int id; private String name; private int age; private Set<Address> addressSet; //get set method } //Address类 public class Address { private int id; private String address; private int tel; private User user; //get set method }
address.hbm.xml <hibernate-mapping package="com.hussion.pojo"> <class name="Address" table="t_address"> <id name="id"> <generator class="native"/> </id> <property name="address"></property> <property name="tel"></property> <many-to-one name="user" class="User" column="userid"/> </class> </hibernate-mapping>
user.hbm.xml <hibernate-mapping package="com.hussion.pojo"> <class name="User" table="t_user"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <property name="age" column="age"></property> <!--inverse="true"放弃关系维护--> <set name="addressSet" inverse="true"> <key column="userid"/> <one-to-many class="Address"/> </set> </class> </hibernate-mapping>
一对一
持久化类设计 public class Person { private int id; private String userName; private Card card; //get set Method } public class Card { private int id; private String cardNum; private Person person; //get set Method }
person.hbm.xml <hibernate-mapping package="com.hussion.pojo"> <class name="Person" table="t_person"> <id name="id"> <generator class="native"></generator> </id> <property name="userName"></property> <one-to-one name="card" class="Card" cascade="delete"/> </class> </hibernate-mapping>
多对多
ublic class Teacher { private int id; private String name; private Set<Student> student; //get set method } public class Student { private int id; private String name; private Set<Teacher> teacher; //get set method }
teacher.hbm.xml <hibernate-mapping package="com.hussion.pojo"> <class name="Teacher" table="t_teacher"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="student" table="t_student_techer"> <key column="teacherid"/> <many-to-many class=“Student” column=“studentid”/> </set> </class> </hibernate-mapping>
student.hbm.xml <hibernate-mapping package="com.hussion.pojo"> <class name="Student" table="t_student"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="teacher" table="t_student_techer"> <key column="studentid"/> <many-to-many class="Teacher" column="teacherid /> </set> </class> </hibernate-mapping>
多对多一方要放弃关系维护 <set name="student" table="t_teacher_student" inverse="true"> 排序 <many-to-many class="Student" column="s_id" order-by="id asc,name desc"></many-to-many>
延迟加载关闭 : lazy="false"
hibernate的数据关联映射,布布扣,bubuko.com
标签:des style blog color strong 数据
原文地址:http://www.cnblogs.com/fudapeng/p/3853174.html