欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

5、hibernate第五课

程序员文章站 2024-03-18 18:24:16
...

Hibernate一对多关系操作

1.先创建 3个实体类

package com.zking.entity;

public class City {
	private String cid;
	private String cname;
	private transient Province province;

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

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

	}

	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 + "]";
	}

}
package com.zking.entity;

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

public class Nation {
	private String nid;
	private String nname;
	private Set<Province> sp = new HashSet<Province>();

	public Nation() {
		super();
	}

	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 + ", sp=" + sp + "]";
	}

}
package com.zking.entity;

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

public class Province {
	private String pid;
	private String pname;
	private transient Nation nation;

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

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

	public Province(String pid, String pname, Nation nation) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.nation = nation;
	}

	public String getPid() {
		return pid;
	}

	public Nation getNation() {
		return nation;
	}

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

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

	public String getPname() {
		return pname;
	}

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

	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 + "]";
	}

}

2.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/books?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/City.hbm.xml" />
		<mapping resource="com/zking/entity/Province.hbm.xml" />
		<mapping resource="com/zking/entity/Nation.hbm.xml" />
	</session-factory>
</hibernate-configuration>

3.配置City.hbm.xml和Nation.hbm.xml和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 8:47:12 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>
		<!-- <property name="pid" type="java.lang.String"> -->
		<!-- <column name="PID" /> -->
		<!-- </property> -->

		<many-to-one name="province" class="com.zking.entity.Province"
			column="pid" ></many-to-one>
	</class>
</hibernate-mapping>
<?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 8:54:51 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="nid"></key>
			<one-to-many class="com.zking.entity.Province" />
		</set>
	</class>
</hibernate-mapping>
<?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 8:54:51 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"
			column="nid" lazy="false"></many-to-one>

		<set name="sc" table="CITY" lazy="false" inverse="true"
			cascade="all-delete-orphan">
			<key column="pid"></key>
			<one-to-many class="com.zking.entity.City" />
		</set>

	</class>
</hibernate-mapping>

4 . 测试类

package com.zking.test;

import java.util.Set;

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

import com.alibaba.fastjson.JSON;
import com.zking.entity.City;
import com.zking.entity.Nation;
import com.zking.entity.Province;

public class TestMany {

	@Test
	public void add() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		// 开始创建插入对象的数据对象
		Nation n1 = new Nation();
		n1.setNname("中国");
		// 省份1
		Province p1 = new Province();
		p1.setPname("湖南");
		// 互设
		p1.setNation(n1);
		// 省份2
		Province p2 = new Province();
		p2.setPname("广东");
		// 互设
		p2.setNation(n1);
		// 国家对象里面添加省份的对象
		n1.getSp().add(p1);
		n1.getSp().add(p2);
		// 保存
		session.save(n1);
		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void query() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Nation n1 = session.get(Nation.class, "a566afb7-b4ea-11e8-9945-54e1ad416ca2");
		System.out.println(n1);
		Province p1 = session.get(Province.class, "a56941f6-b4ea-11e8-9945-54e1ad416ca2");
		System.out.println(p1);
		session.close();
		sessionFactory.close();
	}

	@Test
	public void query2() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Province p1 = session.get(Province.class, "a56941f6-b4ea-11e8-9945-54e1ad416ca2");
		// 城市
		City c1 = new City();
		c1.setCname("长沙");
		c1.setProvince(p1);
		City c2 = new City();
		c2.setCname("株洲");
		c2.setProvince(p1);
		City c3 = new City();
		c3.setCname("湘潭");
		c3.setProvince(p1);
		session.save(c1);
		session.save(c2);
		session.save(c3);
		session.beginTransaction().commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void query3() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Province p1 = session.get(Province.class, "a56941f6-b4ea-11e8-9945-54e1ad416ca2");
		System.out.println(p1.getPname());
		Set<City> sc = p1.getSc();
		for (City city : sc) {
			System.out.println(city);
		}
		session.close();
		sessionFactory.close();
	}

	@Test
	public void query4() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Nation n1 = session.get(Nation.class, "a566afb7-b4ea-11e8-9945-54e1ad416ca2");
		System.out.println(n1.getNname());
		// 省份
		Set<Province> sp = n1.getSp();
		for (Province province : sp) {
			System.out.println(province);
		}
		String str = JSON.toJSONString(n1);
		System.out.println(str);
		session.close();
		sessionFactory.close();
	}

	@Test
	public void delete() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Nation n1 = session.get(Nation.class, "a566afb7-b4ea-11e8-9945-54e1ad416ca2");
		session.delete(n1);
		session.beginTransaction().commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void add2() {
		Configuration config = new Configuration().configure();
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		//国家
		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("湘潭");
		//互设
		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);
		//城市里面设置省份
		c1.setProvince(p1);
		c2.setProvince(p1);
		c3.setProvince(p2);
		session.save(n1);
		session.beginTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}