一对多,一个客户Customer对应多个联系人LinkMan
1.创建表
CREATE TABLE `cst_customer` ( `cust_id` int(11) NOT NULL AUTO_INCREMENT, `cust_name` varchar(255) DEFAULT NULL, `cust_source` varchar(255) DEFAULT NULL, `cust_industry` varchar(255) DEFAULT NULL, `cust_level` varchar(255) DEFAULT NULL, `cust_phone` varchar(255) DEFAULT NULL, `cust_mobile` varchar(255) DEFAULT NULL, PRIMARY KEY (`cust_id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;CREATE TABLE `cst_linkman` ( `lkm_id` int(11) NOT NULL AUTO_INCREMENT, `lkm_name` varchar(255) DEFAULT NULL, `lkm_gender` varchar(255) DEFAULT NULL, `lkm_phone` varchar(255) DEFAULT NULL, `lkm_mobile` varchar(255) DEFAULT NULL, `lkm_email` varchar(255) DEFAULT NULL, `lkm_qq` varchar(255) DEFAULT NULL, `lkm_position` varchar(255) DEFAULT NULL, `lkm_memo` varchar(255) DEFAULT NULL, `lkm_cust_id` int(11) DEFAULT NULL, PRIMARY KEY (`lkm_id`), KEY `FK178F1FE55090E597` (`lkm_cust_id`), CONSTRAINT `FK178F1FE55090E597` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
2.创建和数据对应的实体类
在一的一方,需要配置一个集合装载多方对象,一般用Set, 比如Customer类中配置private Setlink_man = new HashSet<>();
import java.util.HashSet;import java.util.Set;public class Customer { private Integer cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; private Setlink_man = new HashSet<>(); @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + ", link_man=" + link_man + "]"; } public Set getLink_man() { return link_man; } public void setLink_man(Set link_man) { this.link_man = link_man; } public Integer getCust_id() { return cust_id; } public void setCust_id(Integer 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 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_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; } }
多的一方需要配置一方的对象,比如private Customer customer。 public class LinkMan { private Integer lkm_id; private String lkm_name; 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; @Override public String toString() { return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone=" + lkm_phone + ", lkm_mobile=" + lkm_mobile + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq + ", lkm_position=" + lkm_position + ", lkm_memo=" + lkm_memo + "]"; } 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; } }
3.配置实体类对应的配置文件,最重要的是一对多和多对一的配置,cascade设置级联操作,可以是save-update、delete、all
4.编写测试类进行测试
import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.yqg.hibernate.entity.Customer;import com.yqg.hibernate.entity.LinkMan;import com.yqg.hibernate.utils.hibernateUtils;public class Demo3 { @Test //对象导航,双方都要设置级联关系 public void demo3(){ Session session=hibernateUtils.getCurrentSession(); Transaction transaction=session.beginTransaction(); Customer customer=new Customer(); customer.setCust_name("000"); LinkMan linkman=new LinkMan(); linkman.setLkm_name("111"); LinkMan linkman1=new LinkMan(); linkman1.setLkm_name("222"); LinkMan linkman2=new LinkMan(); linkman2.setLkm_name("333"); linkman.setCustomer(customer); customer.getLink_man().add(linkman1); customer.getLink_man().add(linkman2); session.save(linkman);//发送四条插入语句// session.save(customer);//发送两条语句// session.save(linkman2);//发送一句语句 transaction.commit(); }}
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class hibernateUtils { public static final Configuration cfg; public static final SessionFactory sf; static { cfg=new Configuration().configure(); sf=cfg.buildSessionFactory(); } public static Session openSession() { return sf.openSession(); } public static Session getCurrentSession(){ return sf.getCurrentSession(); }}
上面测试了hibernate的对象导航,因为在两者配置文件中设置了cascade=“save-update”,所以可以进行级联保存操作。下面是级联删除操作
import org.hibernate.Session;import org.hibernate.Transaction;/*级联删除 * */import org.junit.Test;import com.yqg.hibernate.entity.Customer;import com.yqg.hibernate.entity.LinkMan;import com.yqg.hibernate.utils.hibernateUtils;public class Demo5 { @Test public void demo() { Session session=hibernateUtils.getCurrentSession(); Transaction transaction=session.beginTransaction(); //如果没有设置级联删除,删除客户那么联系人对应的客户id就为null //设置级联删除,删除customer,级联删除联系人,一般都是删除一方级联删除多方 //Customer customer=(Customer) session.get(Customer.class, 1); System.out.println(customer.toString()); session.delete(customer); //删除多的一方,同时删除一方,基本不用// LinkMan linkman=(LinkMan) session.get(LinkMan.class, 6);// session.delete(linkman); transaction.commit(); }}
5.其他注意点
create 数据库进行操作后创建一个新表
update 数据库进行操作后,更改原来的表
不要忘记在核心配置文件配置资源文件