hibernate_day03_01_表与表之间关系回顾(一对多)

01_表与表之间关系回顾(一对多)



02_表与表之间关系回顾(多对多)




项目结构:



实体类:com.hlg.entity.Customer

package com.hlg.entity;

import java.util.HashSet;
import java.util.Set;

public class Customer {

		//客户id
		private Integer cid;
		//客户名称
		private String custName;
		//客户级别
		private String custLevel;
		//客户来源
		private String custSource;
		//联系电话
		private String custPhone;
		//手机
		private String custMobile;
		
		//在客户实体类里面表示多个联系人,一个客户有多个联系人
		//hibernate要求使用集合表示多的数据,使用set集合
		private Set<LinkMan> setLinkMan = new HashSet<LinkMan>();

		public Integer getCid() {
			return cid;
		}

		public void setCid(Integer cid) {
			this.cid = cid;
		}

		public String getCustName() {
			return custName;
		}

		public void setCustName(String custName) {
			this.custName = custName;
		}

		public String getCustLevel() {
			return custLevel;
		}

		public void setCustLevel(String custLevel) {
			this.custLevel = custLevel;
		}

		public String getCustSource() {
			return custSource;
		}

		public void setCustSource(String custSource) {
			this.custSource = custSource;
		}

		public String getCustPhone() {
			return custPhone;
		}

		public void setCustPhone(String custPhone) {
			this.custPhone = custPhone;
		}

		public String getCustMobile() {
			return custMobile;
		}

		public void setCustMobile(String custMobile) {
			this.custMobile = custMobile;
		}

		public Set<LinkMan> getSetLinkMan() {
			return setLinkMan;
		}

		public void setSetLinkMan(Set<LinkMan> setLinkMan) {
			this.setLinkMan = setLinkMan;
		}
		
		
		
}

实体类: 的配置文件

<?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>
	<!-- 1 配置类和表对应 
		class标签
		name属性:实体类全路径
		table属性:数据库表名称
	-->
	<class name="com.hlg.entity.Customer" table="t_customer">
		<id name="cid" column="cid">
			<generator class="native"></generator>
		</id>
		<property name="custName" column="custName"></property>
		<property name="custLevel" column="custLevel"></property>
		<property name="custSource" column="custSource"></property>
		<property name="custPhone" column="custPhone"></property>
		<property name="custMobile" column="custMobile"></property>
		
		<!-- 在客户映射文件中,表示所有联系人 
			使用set标签表示所有联系人
			set标签里面有name属性:
			     属性值写在客户实体类里面表示联系人的set集合名称
			     
			 inverse属性默认值:false不放弃关系维护
			                true表示放弃关系维护
		-->
		<set name="setLinkMan">
			<!-- 一对多建表,有外键
				hibernate机制:双向维护外键,在一和多那一方都配置外键	
				column属性值:外键名称
			 -->
			<key column="clid"></key>
			<!-- 客户所有的联系人,class里面写联系人实体类全路径 -->
			<one-to-many class="com.hlg.entity.LinkMan"/>
		</set>
	</class>
</hibernate-mapping>

实体类:com.hlg.entity.LinkMan

package com.hlg.entity;

public class LinkMan {

	private Integer lkm_id; // 联系人编号(主键)
	private String lkm_name;// 联系人姓名
	private String lkm_gender;// 联系人性别
	private String lkm_phone;// 联系人办公电话
	
	// 在联系人实体类里面表示所属客户,一个联系人只能属于一个客户
	private 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 Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	
}

实体类:的配置文件


<?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>
	<!-- 1 配置类和表对应 
		class标签
		name属性:实体类全路径
		table属性:数据库表名称
	-->
	<class name="com.hlg.entity.LinkMan" table="t_linkman">
		<id name="lkm_id" column="lkm_id">
			<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>
		
		<!-- 表示联系人所属客户 
			name属性:因为在联系人实体类使用customer对象表示,写customer名称
			class属性:customer全路径
			column属性:外键名称
		-->
		<many-to-one name="customer" class="com.hlg.entity.Customer" column="clid"></many-to-one>
	</class>
</hibernate-mapping>

Hibernate的核心配置文件

<?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://*.36.138:3306/hibernate_day03?userUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull</property>
  	<property name="hibernate.connection.username">root</property>
  	<property name="hibernate.connection.password">*</property>
  	
  	<!-- 第二部分:配置hibernate信息  可选的 -->
  	<!-- 输出底层 sql语句  -->
  	<property name="hibernate.show_sql">true</property>
  	<!-- 输出底层 sql语句的格式  -->
  	<property name="hibernate.format_sql">true</property>
  	<!-- hibernate 创建表,需要配置之后
  		update:如果已经有表,就更新;如果没有,创建
  	 -->
  	<property name="hibernate.hbm2ddl.auto">update</property>
  	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  	
  	<!-- 第三部分 :把映射文件放到核心配置文件中  必须 -->
  	<mapping resource="com/hlg/entity/Customer.hbm.xml"/>
  	<mapping resource="com/hlg/entity/LinkMan.hbm.xml"/>
  	
  </session-factory>

</hibernate-configuration>

工具类:

package com.hlg.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

	static Configuration cfg = null;
	static SessionFactory sessionFactory = null;
	
	//静态代码块实现
	static{
		cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
		
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static void main(String[] args){}
}

测试: 05_一对多操作(级联保存一) com.hlg.hibernatetest.HibernateOnetoMany

package com.hlg.hibernatetest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import com.hlg.entity.Customer;
import com.hlg.entity.LinkMan;
import com.hlg.utils.HibernateUtils;

public class HibernateOnetoMany {

	@Test
	public void testOneToMany(){
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction tx = null;
		
		try{
			sessionFactory = HibernateUtils.getSessionFactory();
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			
			// 添加一个客户,为这个客户添加一个联系人
						//1 创建客户和联系人对象
			Customer customer = new Customer();
			customer.setCustName("传智播客");
			customer.setCustLevel("vip");
			customer.setCustSource("网络");
			customer.setCustPhone("110");
			customer.setCustMobile("999");
						
			LinkMan linkman = new LinkMan();
			linkman.setLkm_name("lucy");
			linkman.setLkm_gender("男");
			linkman.setLkm_phone("911");
						
			//2 在客户表示所有联系人,在联系人表示客户		
			// 建立客户对象和联系人对象关系
			//2.1 把联系人对象 放到客户对象的set集合里面
			customer.getSetLinkMan().add(linkman);
			//2.2 把客户对象放到联系人里面
			linkman.setCustomer(customer);
						
			//3 保存到数据库
			session.save(customer);
			session.save(linkman);
			
			//提交事务
			tx.commit();
			
		}catch(Exception e){
			//回滚事务
			tx.rollback();
		}finally{
			//关闭
			session.close();
			sessionFactory.close();
		}
	}
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值