hibernate-5-映射

来源:互联网 发布:怎样才可以开淘宝店 编辑:程序博客网 时间:2024/06/08 03:09

一对一实体类:

package com.cascade.entity.oneToMany;import java.util.HashSet;import java.util.Set;public class Customer {private Integer id;private String name;private Set<Order> orders = new HashSet<Order>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Order> getOrders() {return orders;}public void setOrders(Set<Order> orders) {this.orders = orders;}}


package com.cascade.entity.oneToMany;public class Order {private Integer id;private String address;private Double price;private Customer customer;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}}

一对一xml配置:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-mapping><class name="com.cascade.entity.oneToMany.Customer" table="customer"><id name="id" column="id"><generator class="native"></generator></id><!-- 普通属性 --><property name="name" length="20" /><!-- 一对多映射 --><set name="orders" cascade="save-update,delete-orphan"><!-- 确定关联的外键列 --><key column="cid" /><!-- 映射到关联的属性 --><one-to-many class="com.cascade.entity.oneToMany.Order"/></set><!-- 还有另一种方式配置  在Customer.hbm.xml中是one-to-may 那么在Order.hbm.xml中就是many-to-one 如下: 两种选其一效果都一样 --><!-- 多对一映射 --><!-- <many-to-one name="customer" class="com.entity.threeMapRelationXML.oneToMany.Customer" column="cid"/> --></class><class name="com.cascade.entity.oneToMany.Order" table="orders"><id name="id" column="id"><generator class="native"></generator></id><!-- 普通属性 --><property name="address" length="50" /><property name="price"/><!-- 多对一映射 --><many-to-one name="customer" class="com.cascade.entity.oneToMany.Customer" column="cid"/></class></hibernate-mapping>



多对多实体类:

package com.cascade.entity.manyToMany;import java.util.HashSet;import java.util.Set;public class Student {private Integer id;private String sname;private Set<Course> courses = new HashSet<Course>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public Set<Course> getCourses() {return courses;}public void setCourses(Set<Course> courses) {this.courses = courses;}}

package com.cascade.entity.manyToMany;import java.util.HashSet;import java.util.Set;public class Course {private Integer id;private String cname;private Set<Student> students = new HashSet<Student>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}

多对多xml配置:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-mapping><class name="com.cascade.entity.manyToMany.Course" table="course" ><id name="id" column="id"><generator class="native"></generator></id><!-- 普通属性 --><property name="cname" length="30" /><!-- 一对多映射 --><set name="students" table="s_c" cascade="save-update,delete"><!-- 确定关联的外键列 --><key column="cid" /><!-- 映射到关联的属性 --><many-to-many  class="com.cascade.entity.manyToMany.Student" column="sid"/></set></class><class name="com.cascade.entity.manyToMany.Student" table="student"><id name="id" column="id"><generator class="native"></generator></id><!-- 普通属性 --><property name="sname" length="30" /><set name="courses" table="s_c" cascade="save-update,delete"><!-- 确定关联的外键列 --><key column="sid" /><!-- 映射到关联的属性 --><many-to-many  class="com.cascade.entity.manyToMany.Course" column="cid"/></set></class></hibernate-mapping>


测试类:
package com.test;import org.hibernate.Session;import org.hibernate.Transaction;import com.cascade.entity.manyToMany.Course;import com.cascade.entity.manyToMany.Student;import com.cascade.entity.oneToMany.Customer;import com.cascade.entity.oneToMany.Order;import com.util.GetHibernateSessionFactory;public class Test3 {public static void main(String[] args) {//test01();//test10();//test30();//test31();//test32();//test33();test34();}/** * 方法的作用 : 一对多  多对一 案例一 * 创建人:曹雪坤 */public static void test01(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Customer c = new Customer();c.setName("aaa");Order o = new Order();o.setAddress("bei");o.setPrice(10000d);o.setCustomer(c);c.getOrders().add(o);session.save(c);session.save(o);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}/** * 方法的作用 : 多对多 案例一 * 创建人:曹雪坤 */public static void test10(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Student s1 = new Student();s1.setSname("大鸟");Student s2 = new Student();s2.setSname("小鸟");Course c1 = new Course();c1.setCname("数学");Course c2 = new Course();c2.setCname("language");s1.getCourses().add(c1);s1.getCourses().add(c2);s2.getCourses().add(c1);s2.getCourses().add(c2);session.save(s1);session.save(s2);session.save(c1);session.save(c2);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}/** * 级联(当主控放执行保存,更新,删除操作时,其关联的对象也执行相同的操作,在映射文件中使用cascade属性设置) * cascade属性值: * save-update:在执行save,update或saveOrUpdate是进行关联操作 * delete:在执行delete时进行关联操作 * delete-orphan:表示孤儿删除,即删除所有和当前对象解除关联关系的对象 * all:所有情况下均进行关联操作,单不包含delete-orphan操作 * none:所用情况下都不进行关联操作 * all-delete-orphan:所有情况下均进行关联操作  *//** * 方法的作用 : 一对多级联保存 * 创建人:曹雪坤 */public static void test30(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Customer c1 = new Customer();c1.setName("李四");Order o = new Order();o.setAddress("上海");o.setPrice(10d);//客户关联订单c1.getOrders().add(o);//仅保存客户session.save(c1);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();//在CustomerOrder.hbm.xml中配置cascade="save-update"当在save(),update,saveOrUpdate()将c1编程持久态时,也将o变成持久态//由于没有在xml中配置关联所以报错:/** * 单词: * reference(vt):引用 * transient:临时的 * instance(n):实例 * 下面的含义是一个对象引用了另一个对象(该对象是瞬时态的(临时的),这种情况是hibernate默认情况下是不允许的,需要在xml中配置) * org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.entity.threeMapRelationXML.oneToMany.Order */}/** * 方法的作用 : 一对多级联删除 cascade可以设定多个值用","分割,将cascade="save-update,delete" * 创建人:曹雪坤 */public static void test31(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Customer c = (Customer) session.get(Customer.class, 1);//cascade="save-update,delete" 故删除c与c相关的Order便也删除了session.delete(c);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}/** * 方法的作用 : 孤儿删除(好像和cascade="delete",没有什么区别) * 孤儿删除:如果cascade没有设置有关的级联删除,那么在删除Customer实体时, * 对应的Order实体表的cid属性就会为null,这是我们将这些cid为null所对应的数据称为孤儿 * 创建人:曹雪坤 */public static  void test32(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();//将cascade="delete-orphan"//这两个对象是相关联的Customer c = (Customer) session.get(Customer.class, 12);Order o = (Order) session.get(Order.class, 10);//当解除关系的时候,将二者都删掉了c.getOrders().remove(o);session.delete(c);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}/** * 方法的作用 : 多对多级联保存 * 创建人:曹雪坤 */public static void test33(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Student s1 = new Student();Course c1 = new Course();s1.setSname("大神");c1.setCname("神学");s1.getCourses().add(c1);//你如果只在student对应表的xml中设置级联,那么在保存student实例的时候可以级联course实例,但是如果你保存crouse实例的时候,却无法级联保存student//所以最好两个xml都写上级联session.save(s1);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}/** * 方法的作用 : 多对多级联删除 * 创建人:曹雪坤 */public static void test34(){Session session = GetHibernateSessionFactory.sessionFactory.openSession();Transaction t = session.beginTransaction();Student s1 = (Student) session.get(Student.class,1);session.delete(s1);t.commit();session.close();GetHibernateSessionFactory.sessionFactory.close();}}



原创粉丝点击