Hibernate的关系映射——Ont-To-Many(一对多、多对一)

使用一个例子来表明一对多和多对一的关系(国家、省份和城市之间的关系)

一个国家有多个省份(一对多),多个省份属于一个国家(多对一)

一个省份有多个城市(一对多),多个城市属于一个省份(多对一)

首先,创建三个实体类分别是:Nation(国家),Province(省份),City(城市)

Ont-To-Many:定义多端Set集合,需要实例化HashSet

Many-To-Ont:定义一端对象

Nation(国家)实体类:

public class Nation {

	private String nid;
	private String nname;

	private Set<Province> sp = new HashSet<>();

	public Nation() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Nation(String nid, String nname, Set<Province> sp) {
		super();
		this.nid = nid;
		this.nname = nname;
		this.sp = sp;
	}

	public String getNid() {
		return nid;
	}

	public void setNid(String nid) {
		this.nid = nid;
	}

	public String getNname() {
		return nname;
	}

	public void setNname(String nname) {
		this.nname = nname;
	}

	public Set<Province> getSp() {
		return sp;
	}

	public void setSp(Set<Province> sp) {
		this.sp = sp;
	}

	@Override
	public String toString() {
		return "Nation [nid=" + nid + ", nname=" + nname + "]";
	}

Province(省份)实体类:

public class Province {

	private String pid;
	private String pname;
	private Nation nation;

	private Set<City> sc = new HashSet<>();

	public Province() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Province(String pid, String pname, Nation nation, Set<City> sc) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.nation = nation;
		this.sc = sc;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public Nation getNation() {
		return nation;
	}

	public void setNation(Nation nation) {
		this.nation = nation;
	}

	public Set<City> getSc() {
		return sc;
	}

	public void setSc(Set<City> sc) {
		this.sc = sc;
	}

	@Override
	public String toString() {
		return "Province [pid=" + pid + ", pname=" + pname + "]";
	}

City(城市)实体类:

public class City {

	private String cid;
	private String cname;
	private Province province;

	public City() {
		super();
		// TODO Auto-generated constructor stub
	}

	public City(String cid, String cname, Province province) {
		super();
		this.cid = cid;
		this.cname = cname;
		this.province = province;
	}

	public String getCid() {
		return cid;
	}

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

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	@Override
	public String toString() {
		return "City [cid=" + cid + ", cname=" + cname + "]";
	}

配置三个实体类的映射文件

           

Nation.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-10 18:29:08 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
	<class name="com.zking.entity.Nation" table="NATION">
		<id name="nid" type="java.lang.String">
			<column name="NID" />
			<generator class="guid" />
		</id>
		<property name="nname" type="java.lang.String">
			<column name="NNAME" />
		</property>
		<set name="sp" table="PROVINCE" cascade="all-delete-orphan"
			inverse="true" lazy="false">
			<key>
				<column name="NID" />
			</key>
			<one-to-many class="com.zking.entity.Province" />
		</set>
	</class>
</hibernate-mapping>

Province.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-10 18:29:08 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
	<class name="com.zking.entity.Province" table="PROVINCE">
		<id name="pid" type="java.lang.String">
			<column name="PID" />
			<generator class="guid" />
		</id>
		<property name="pname" type="java.lang.String">
			<column name="PNAME" />
		</property>
		<many-to-one name="nation" class="com.zking.entity.Nation"
			fetch="join">
			<column name="nid" />
		</many-to-one>
		<set name="sc" table="CITY" cascade="all-delete-orphan" inverse="true"
			lazy="false">
			<key>
				<column name="PID" />
			</key>
			<one-to-many class="com.zking.entity.City" />
		</set>
	</class>
</hibernate-mapping>

City.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-9-10 18:29:08 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
	<class name="com.zking.entity.City" table="CITY">
		<id name="cid" type="java.lang.String">
			<column name="CID" />
			<generator class="guid" />
		</id>
		<property name="cname" type="java.lang.String">
			<column name="CNAME" />
		</property>
		<many-to-one name="province" class="com.zking.entity.Province"
			fetch="join">
			<column name="pid" />
		</many-to-one>
	</class>
</hibernate-mapping>

生成一个唯一的Hibernate映射文件(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.password">sasa</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/t204?characterEncoding=UTF-8</property>
		<property name="hibernate.connection.username">root</property>

		<property name="show_sql">true</property>

		<property name="format_sql">true</property>

		<mapping resource="com/zking/entity/Nation.hbm.xml" />

		<mapping resource="com/zking/entity/Province.hbm.xml" />

		<mapping resource="com/zking/entity/City.hbm.xml" />
	</session-factory>
</hibernate-configuration>

大体的创建如下:

最后写一个测试的类(OneToMany.java)级联增加:

public class OneToMany {

	@Test
	public void add() {
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		// 国家
		Nation n1 = new Nation();
		n1.setNname("中国");

		// 省份
		Province p1 = new Province();
		p1.setPname("湖南");
		Province p2 = new Province();
		p2.setPname("广东");

		// 城市
		City c1 = new City();
		c1.setCname("长沙");
		City c2 = new City();
		c2.setCname("怀化");
		City c3 = new City();
		c3.setCname("衡阳");
		City c4 = new City();
		c4.setCname("广州");
		City c5 = new City();
		c5.setCname("深圳");

		// 互设
		// 国家里面加省份
		n1.getSp().add(p1);
		n1.getSp().add(p2);

		// 省份里面设置国家
		p1.setNation(n1);
		p2.setNation(n1);

		// 省份里面加城市
		p1.getSc().add(c1);
		p1.getSc().add(c2);
		p1.getSc().add(c3);

		p2.getSc().add(c4);
		p2.getSc().add(c5);

		// 城市里面设置省份
		c1.setProvince(p1);
		c2.setProvince(p1);
		c3.setProvince(p1);

		c4.setProvince(p2);
		c5.setProvince(p2);

		// 保存
		session.save(n1);

		transaction.commit();
		session.close();
		sessionFactory.close();
	}

}

最后的效果如下:Nation(国家)表

Province(省份)表:

City(城市)表:

三表的级联删除:

public class OneToMany {

	@Test
	public void delete() {
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Nation n1 = session.get(Nation.class, "472829bf-b4e3-11e8-bcfa-88d7f6a16255");
		session.delete(n1);
		transaction.commit();
		session.close();
		sessionFactory.close();
	}

}

删除效果则是三表相关联的同一国家的数据都会被删除。

展开阅读全文

没有更多推荐了,返回首页