hibernate级联增删改查
来源:互联网 发布:网络推广找 三尾狐 编辑:程序博客网 时间:2024/06/07 02:48
1、model包Customer.java
package model;import java.util.HashSet;import java.util.Set;public class Customer {private Long cust_id;private String cust_name;private Long cust_user_id;private Long cust_create_id;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_mobile;//set集合表示多个联系人private Set<LinkMan> linkMans = new HashSet<>();public Set<LinkMan> getLinkMans() {return linkMans;}public void setLinkMans(Set<LinkMan> linkMans) {this.linkMans = linkMans;}public Long getCust_id() {return cust_id;}public void setCust_id(Long cust_id) {this.cust_id = cust_id;}public String getCust_name() {return cust_name;}public void setCust_name(String cust_name) {this.cust_name = cust_name;}public Long getCust_user_id() {return cust_user_id;}public void setCust_user_id(Long cust_user_id) {this.cust_user_id = cust_user_id;}public Long getCust_create_id() {return cust_create_id;}public void setCust_create_id(Long cust_create_id) {this.cust_create_id = cust_create_id;}public String getCust_source() {return cust_source;}public void setCust_source(String cust_source) {this.cust_source = cust_source;}public String getCust_industry() {return cust_industry;}public void setCust_industry(String cust_industry) {this.cust_industry = cust_industry;}public String getCust_level() {return cust_level;}public void setCust_level(String cust_level) {this.cust_level = cust_level;}public String getCust_linkman() {return cust_linkman;}public void setCust_linkman(String cust_linkman) {this.cust_linkman = cust_linkman;}public String getCust_phone() {return cust_phone;}public void setCust_phone(String cust_phone) {this.cust_phone = cust_phone;}public String getCust_mobile() {return cust_mobile;}public void setCust_mobile(String cust_mobile) {this.cust_mobile = cust_mobile;}}2、model包Customer.hbm.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"><hibernate-mapping><!-- javabean与表之间的对应关系 --><class name="model.Customer" table="cst_customer"><!-- 主键对应 --><id name="cust_id" column="cust_id"><!-- 主键策略(与自增长相关) --><!-- hibernate框架来产生主键 整形 --><!-- increment 在多线程情况下不能用这种方式 --><!-- identity 主键由数据库产生,适用于mysql --><!-- oracle 主键由数据库产生,适用于oracle --><!-- uuid 产生32位随机数,String类型,可以在多线程情况下使用--><!-- assigned:唯一的自然主键生成方式,需要手动去设置主键--><!-- 默认:native --><generator class="native"></generator></id><!-- 其他字段 --><property name="cust_name" column="cust_name"></property><property name="cust_user_id" column="cust_user_id"></property><property name="cust_create_id" column="cust_create_id"></property><property name="cust_source" column="cust_source"></property><property name="cust_industry" column="cust_industry"></property><property name="cust_level" column="cust_level"></property><property name="cust_linkman" column="cust_linkman"></property><property name="cust_phone" column="cust_phone"></property><property name="cust_mobile" column="cust_mobile"></property><!-- 配置一对多 --><!-- 配置级联保存:让瞬时态对象变成持久态 cascade="save-update"--><!-- 级联删除:cascade="delete"配置在一方--><!-- 配置孤儿删除:cascade="delete-orphan" --><!-- inverse="true"放弃外键维护 --><!-- cascade用来级联操作(保存、修改和删除)在一方设置 --><!-- inverse用来维护外键的 在一方设置 --><set name="linkMans" inverse="true"><!-- 外键 --><key column="lkm_cust_id"></key><!-- 一对多关系 --><one-to-many class="model.LinkMan"/></set></class></hibernate-mapping>3、model包LinkMan.java
package model;public class LinkMan {private Integer lkm_id;private String lkm_name;//lkm_cust_id 外键,不用写private String lkm_gender;private String lkm_phone;private String lkm_mobile;private String lkm_email;private String lkm_qq;private String lkm_position;private String lkm_memo;//客户类private Customer customer;public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}public Integer getLkm_id() {return lkm_id;}public void setLkm_id(Integer lkm_id) {this.lkm_id = lkm_id;}public String getLkm_name() {return lkm_name;}public void setLkm_name(String lkm_name) {this.lkm_name = lkm_name;}public String getLkm_gender() {return lkm_gender;}public void setLkm_gender(String lkm_gender) {this.lkm_gender = lkm_gender;}public String getLkm_phone() {return lkm_phone;}public void setLkm_phone(String lkm_phone) {this.lkm_phone = lkm_phone;}public String getLkm_mobile() {return lkm_mobile;}public void setLkm_mobile(String lkm_mobile) {this.lkm_mobile = lkm_mobile;}public String getLkm_email() {return lkm_email;}public void setLkm_email(String lkm_email) {this.lkm_email = lkm_email;}public String getLkm_qq() {return lkm_qq;}public void setLkm_qq(String lkm_qq) {this.lkm_qq = lkm_qq;}public String getLkm_position() {return lkm_position;}public void setLkm_position(String lkm_position) {this.lkm_position = lkm_position;}public String getLkm_memo() {return lkm_memo;}public void setLkm_memo(String lkm_memo) {this.lkm_memo = lkm_memo;}}4、model包LinkMan.hbm.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"><hibernate-mapping><!-- javabean与表之间的对应关系 --><class name="model.LinkMan" table="cst_linkman"><!-- 主键对应 --><id name="lkm_id" column="lkm_id"><!-- 主键策略(与自增长相关) --><!-- hibernate框架来产生主键 整形 --><!-- increment 在多线程情况下不能用这种方式 --><!-- identity 主键由数据库产生,适用于mysql --><!-- oracle 主键由数据库产生,适用于oracle --><!-- uuid 产生32位随机数,String类型,可以在多线程情况下使用--><!-- assigned:唯一的自然主键生成方式,需要手动去设置主键--><generator class="native"></generator></id><!-- 其他字段 --><property name="lkm_name" column="lkm_name"></property><property name="lkm_gender" column="lkm_gender"></property><property name="lkm_phone" column="lkm_phone"></property><property name="lkm_mobile" column="lkm_mobile"></property><property name="lkm_email" column="lkm_email"></property><property name="lkm_qq" column="lkm_qq"></property><property name="lkm_position" column="lkm_position"></property><property name="lkm_memo" column="lkm_memo"></property><!-- 多方配置 --><!-- name:LinkMan javabean中的客户属性 --><!-- class:一方的javabean --><!-- column:外键 --><!-- 配置级联保存,配置在多方有利于提升效率 cascade="save-update" 把瞬时态客户变为持久态 --><!-- 配置删除:cascade="delete" --><many-to-one name="customer" class="model.Customer" column="lkm_cust_id" cascade="save-update"></many-to-one></class></hibernate-mapping>5、工具类HibernateUtils.java
package util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils {//使用一个静态块创建SessionFactoryprivate static Configuration CONFIG;private static SessionFactory FACTORY;static {CONFIG = new Configuration().configure();FACTORY = CONFIG.buildSessionFactory();}//从连接池中获取连接public static Session openSession() {return FACTORY.openSession();}//session绑定到线程public static Session getCurrentSession() {return FACTORY.getCurrentSession();}}6、hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><!-- 配置连接信息 --><session-factory><!-- 必选配置 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/20171203hibernatetest</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">1234</property><!-- 数据库方言 使用的数据库类型 --><property name="hibernate.dialect org.hibernate.dialect.MySQLDialect"></property><!-- 可选配置 --><!-- 显示sql --><property name="hibernate.show_sql">true</property><!-- 格式化sql --><property name="hibernate.format_sql">true</property><!-- 配置c3p0连接池 --><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!-- 最小连接数 --><property name="hibernate.c3p0.min_size">5</property><!-- 最大连接数 --><property name="hibernate.c3p0.max_size">20</property><!-- 最大空闲连接120秒 --><property name="hibernate.c3p0.timeout">120</property><!-- 自动建表 删除,创建,删除:creat-drop 创建:creat 如果没有表就创建,有就同步:update(一般使用) 验证类和数据表结构是否一致:validate--><property name="hibernate.hbm2ddl.auto">update</property><!-- 把session绑定到当前线程,使用getCurrentSession获取当前session --><property name="hibernate.current_session_context_class">thread</property><!-- 映射文件 --><mapping resource="model/Customer.hbm.xml"/><mapping resource="model/LinkMan.hbm.xml"/></session-factory></hibernate-configuration>7、测试类TestOneToMany.java
package test;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import model.Customer;import model.LinkMan;import util.HibernateUtils;public class TestOneToMany {@Testpublic void testSave() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();//新建一个客户和2个联系人Customer c = new Customer();c.setCust_name("马云");LinkMan m1 = new LinkMan();m1.setLkm_name("强哥");LinkMan m2 = new LinkMan();m2.setLkm_name("小宋");//双向关联//客户关联联系人c.getLinkMans().add(m1);c.getLinkMans().add(m2);//联系人关联客户m1.setCustomer(c);m2.setCustomer(c);//保存session.save(c);session.save(m1);session.save(m2);tr.commit();}@Testpublic void testSave1() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();//新建一个客户和2个联系人Customer c = new Customer();c.setCust_name("马云1");LinkMan m1 = new LinkMan();m1.setLkm_name("强哥1");LinkMan m2 = new LinkMan();m2.setLkm_name("小宋1");//单向关联//客户关联联系人c.getLinkMans().add(m1);c.getLinkMans().add(m2);//单向保存session.save(c);tr.commit();}@Testpublic void testSave2() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();//新建一个客户和2个联系人Customer c = new Customer();c.setCust_name("马云2");LinkMan m1 = new LinkMan();m1.setLkm_name("强哥2");LinkMan m2 = new LinkMan();m2.setLkm_name("小宋2");//单向关联//联系人关联客户m1.setCustomer(c);m2.setCustomer(c);//单向保存session.save(m1);session.save(m2);tr.commit();}@Testpublic void testDelete() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();Customer c = session.get(Customer.class, 9L);session.delete(c);tr.commit();}@Testpublic void testDelete1() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();LinkMan linkMan = session.get(LinkMan.class, 8);System.out.println(linkMan.getCustomer().getCust_id());session.delete(linkMan);tr.commit();}//孤儿删除@Testpublic void testDelete2() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();//获取一个客户Customer c = session.get(Customer.class, 18L);//获取2号联系人LinkMan m2 = session.get(LinkMan.class, 9);//解除关系c.getLinkMans().remove(m2);//由快照功能tr.commit();}//cascade用来级联操作(保存、修改和删除)在一方设置//inverse用来维护外键的 在一方设置@Testpublic void testSave5() {Session session = HibernateUtils.getCurrentSession();Transaction tr = session.beginTransaction();//新建一个客户和2个联系人Customer c = session.get(Customer.class, 43L);LinkMan m = session.get(LinkMan.class,22);//双向关联c.getLinkMans().add(m);m.setCustomer(c);session.save(c);session.save(m);tr.commit();}}
阅读全文