时间:2021-07-01 10:21:17 帮助过:37人阅读
3.person.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!--导入dbd约束--> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bean"> <class name="Person" table="person"> <id name="pId" column="p_id"> <generator class="native"></generator> </id> <property name="pName" column="p_name"></property> <property name="pAddress" column="p_address"></property> <!--一对多关系映射:从表实体的映射配置 涉及的标签: many-to-one 作用:建立多对一的映射配置 属性:name 从实体中映入主表实体对象的引用名称 class 指定属性对应的实体类名称 column 指定从表中外键字段的名称 --> <many-to-one name="role" class="Role" column="p_r_id"></many-to-one> </class> </hibernate-mapping>
4.role.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!--导入dbd约束--> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bean"> <class name="Role" table="role"> <id name="rId" column="r_id"> <generator class="native"></generator> </id> <property name="rName" column="r_name"></property> <!--一对多关系映射:主表实体的映射配置 涉及的标签: set/list key many-to-one 作用:用于配置set/list集合属性 属性:name 指定实体中set/list集合属性名称 table 指定从表名称。一对多配置可以补血 key作用:用于映射外键字段 属性:column 指定外键名称 many-to-one作用:建立一对多的映射配置 属性:calss 指定从表实体的名称 cascade="save-update" 级联更新为true 级联删除需要配置inverse="true" cascade="save-update,delete"(开发中一般慎用) --> <set name="set" table="person" inverse="true" cascade="save-update,delete"> <key column="p_r_id" ></key> <one-to-many class="Person"></one-to-many> </set> </class> </hibernate-mapping>
5.实体类
public class Person implements Serializable { private Integer pId; private String pName; private String pAddress; //一对多关系映射,从表实体包含主表实体的对象引用 private Role role; } //省了get/set方法
public class Role implements Serializable { private Integer rId; private String rName; //一对多关系映射,主表实体应该包含从表实体的集引用 private Set<Person> set; } //省去get/set方法
6.测试类
public class TestHibernate01 { //查询Person @Test public void test01() { Session s = HibernateUtils.getCurrnetSession(); Transaction tx = s.beginTransaction(); Query query = s.createQuery("from Person "); List list = query.list(); for (Object o : list) { Person person = (Person) o; System.out.println(person.getpName() + ":" + person.getRole().getrName()); } tx.commit(); } //查询所有的Role @Test public void test02() { Session s = HibernateUtils.getCurrnetSession(); Transaction tx = s.beginTransaction(); Query query = s.createQuery("from Role"); List list = query.list(); for (Object o : list) { Role role = (Role) o; System.out.println(role.getrName()); for (Person person : role.getSet()) { System.out.println("set:" + person.getpName()); } } tx.commit(); } //修改角色的操作保存操作 @Test public void test03() { Session s = HibernateUtils.getCurrnetSession(); Transaction tx = s.beginTransaction(); Role o = (Role) s.get(Role.class, 1); System.out.println("Role=" + o); Person o1 = (Person) s.get(Person.class, 2); o1.setRole(o); System.out.println("Person=" + o1); s.update(o1); tx.commit(); } //级联更新 // set标签的属性cascade="save-update" 级联更新为true @Test public void test04() { Session s = HibernateUtils.getCurrnetSession(); Transaction tx = s.beginTransaction(); Role o = (Role) s.get(Role.class, 3); o.setrName("大将"); System.out.println("Role=" + o.getrName()); s.save(o); tx.commit(); } //级联删除 // set标签 级联删除需要配置inverse="true" cascade="save-update,delete"(开发中一般慎用) @Test public void test05() { Session s = HibernateUtils.getCurrnetSession(); Transaction tx = s.beginTransaction(); Role o = (Role) s.get(Role.class, 3); s.delete(o); tx.commit(); } }
数据库表关系及配置
标签:作用 img type imp user 主表 factor string print