Hibernate一对多+级联操作

1、建立一个实体类

package hibernate;
import java.util.HashSet;
import java.util.Set;
public class Customer {
	private Integer cid;
	private String custName;
	private String custLevel;
	private String custSource;
	private String custMobile;
	//一对多的那个类,hibernate要求用Set
	private Set<LinkMan> setLinkMan = new HashSet<LinkMan>();
	
	public Set<LinkMan> getSetLinkMan() {
		return setLinkMan;
	}
	public void setSetLinkMan(Set<LinkMan> setLinkMan) {
		this.setLinkMan = setLinkMan;
	}
	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 getCustMobile() {
		return custMobile;
	}
	public void setCustMobile(String custMobile) {
		this.custMobile = custMobile;
	}
}

2、建立另外一个实体类

package hibernate;
public class LinkMan {
	private Integer lkm_id;
	private String lkm_name;
	private String lkm_gender;
	private String lkm_phone;
	//和它有多对一关系的类
	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;
	}	
}

3、一个类的映射关系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>
	<class name="hibernate.Customer" table="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="custMobile" column="custMobile"></property>
		
		<set name="setLinkMan">
			<!-- column是外键名称 -->
			<key column="clid"></key>
			<one-to-many class="hibernate.LinkMan" />
		</set>
		
	</class>
</hibernate-mapping>

4、另外一个类的映射关系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>
	<class name="hibernate.LinkMan" table="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>
		
		<many-to-one name="customer" class="hibernate.Customer" column="clid">
		</many-to-one>
		
	</class>
</hibernate-mapping>

5、核心配置文件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>
		<!-- 1、配置数据库信息 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		
		<!-- 2、配置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>
		 <!-- 配置数据库方言
		 	 让hibernate识别不同数据库语句-->
		 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		 
		 <!-- 3、把映射文件放到核心配置文件中 -->
		 <mapping resource="hibernate/Customer.hbm.xml"></mapping>
		 <mapping resource="hibernate/LinkMan.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

6、测试文件

package hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class TestHibernate {
	@Test
	public void test()
	{
		//1、加载hibernate核心配置文件
		Configuration cfg = new Configuration();
		cfg.configure();
		//2、创建SessionFactory对象
		//读取核心配置文件,创建sessionFactory
		//根据映射关系创建表
		SessionFactory sessionFacroty =  cfg.buildSessionFactory();
		//3、创建session对象
		//类似于创建连接
		Session session = sessionFacroty.openSession();
		//4、开启事务
		org.hibernate.Transaction tx = session.beginTransaction();
		//5、写具体crud操作
		
		Customer customer = new Customer();
		customer.setCustName("百度");
		customer.setCustLevel("vip");
		customer.setCustSource("网络");
		customer.setCustMobile("999");
		
		LinkMan linkMan = new LinkMan();
		linkMan.setLkm_name("lucy");
		linkMan.setLkm_gender("男");
		linkMan.setLkm_phone("911");
		//建立级联关系!
		customer.getSetLinkMan().add(linkMan);
		linkMan.setCustomer(customer);
		
		session.save(customer);
		session.save(linkMan);
		
		//6、提交事务
		tx.commit();
		//7、关闭资源
		session.close();
		sessionFacroty.close();
		
	}
}


ps:简化版本

1、一对多的一的映射xml文件


2、测试文件里可以注释掉两行


猜你喜欢

转载自blog.csdn.net/sanmao123456_/article/details/80849357