以下是基于省份对应多个城市的映射配置及级联CURD例子
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.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property><!-- 显示sql -->
<property name="format_sql">true</property><!-- 格式化sql -->
<mapping resource="club/lygangdai/entity/Province.hbm.xml"/>
<mapping resource="club/lygangdai/entity/City.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Province.java
package club.lygangdai.entity;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName: Province
* @Description: TODO(省份类)
* @author Uncle
* @date 2018年8月7日 上午9:50:48
*
*/
public class Province {
private int pid; //id
private String pname; //省份
private Set<City> cities = new HashSet<City>();//城市集合
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @param pid
* @param pname
*/
public Province(int pid, String pname) {
super();
this.pid = pid;
this.pname = pname;
}
/**
* @return cities
*/
public Set<City> getCities() {
return cities;
}
/**
* @param cities 要设置的 cities
*/
public void setCities(Set<City> cities) {
this.cities = cities;
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public Province() {
super();
}
/**
* @return pid
*/
public int getPid() {
return pid;
}
/**
* @param pid 要设置的 pid
*/
public void setPid(int pid) {
this.pid = pid;
}
/**
* @return pname
*/
public String getPname() {
return pname;
}
/**
* @param pname 要设置的 pname
*/
public void setPname(String pname) {
this.pname = pname;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Province [pid=" + pid + ", pname=" + pname + "]";
}
}
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??8??2?? ????1:45:51 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!--映射实体类对象,生成默认为province的数据库表,lazy关闭延迟加载-->
<class name="club.lygangdai.entity.Province" table="province" lazy="false">
<id name="pid" type="int">
<column name="pid"></column>
<!-- 自增 -->
<generator class="native"></generator>
</id>
<property name="pname" type="java.lang.String">
<column name="pname"></column>
</property>
<!-- province里的set集合,对应的city表 ,inverse关联关系交给多端维护-->
<set name="cities" table="city" cascade="all-delete-orphan" inverse="true">
<!-- city表中与province对应的列,city表外键 pid-->
<key column="pid"></key>
<!-- 对应多端实体类 -->
<one-to-many class="club.lygangdai.entity.City"/>
</set>
</class>
</hibernate-mapping>
City.java
package club.lygangdai.entity;
/**
* @ClassName: city
* @Description: TODO(城市类)
* @author Uncle
* @date 2018年8月7日 上午9:40:17
*
*/
public class City {
private int cid; //id
private String cname; //城市
private int pid; //省份id
private Province p; //省份对象
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public City() {
super();
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @param cid
* @param cname
* @param pid
*/
public City(int cid, String cname, int pid) {
super();
this.cid = cid;
this.cname = cname;
this.pid = pid;
}
/**
* @return p
*/
public Province getP() {
return p;
}
/**
* @param p 要设置的 p
*/
public void setP(Province p) {
this.p = p;
}
/**
* @return cid
*/
public int getCid() {
return cid;
}
/**
* @param cid 要设置的 cid
*/
public void setCid(int cid) {
this.cid = cid;
}
/**
* @return cname
*/
public String getCname() {
return cname;
}
/**
* @param cname 要设置的 cname
*/
public void setCname(String cname) {
this.cname = cname;
}
/**
* @return pid
*/
public int getPid() {
return pid;
}
/**
* @param pid 要设置的 pid
*/
public void setPid(int pid) {
this.pid = pid;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "city [cid=" + cid + ", cname=" + cname + ", pid=" + pid + "]";
}
}
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??8??2?? ????1:45:51 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!--映射实体类对象,生成默认为city的数据库表,lazy关闭延迟加载-->
<class name="club.lygangdai.entity.City" table="city" lazy="false">
<id name="cid" type="int">
<column name="cid"></column>
<generator class="native"></generator>
</id>
<property name="cname" type="java.lang.String">
<column name="cname"></column>
</property>
<!-- 一端的对象,对应外键 pid -->
<many-to-one name="p" class="club.lygangdai.entity.Province" column="pid"></many-to-one>
</class>
</hibernate-mapping>
HibernateTest.java
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import club.lygangdai.entity.Card;
import club.lygangdai.entity.City;
import club.lygangdai.entity.Person;
import club.lygangdai.entity.Province;
/**
* @ClassName: HibernateTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Uncle
* @date 2018年8月7日 上午8:58:50
*
*/
public class HibernateTest {
@Test
public void hibernateTest() {
//读取配置文件
Configuration configuration = new Configuration().configure();
//创建工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
//session
Session session = sessionFactory.openSession();
//事务
Transaction transaction = session.beginTransaction();
//级联关系
//一对多-----------------------------------------------------------------------------------
//添加
// Province p = new Province();
// p.setPname("湖南");
//
// City c1 = new City();
// c1.setCname("长沙");
// City c2 = new City();
// c2.setCname("株洲");
// City c3 = new City();
// c3.setCname("湘潭");
//
// p.getCities().add(c1);
// p.getCities().add(c2);
// p.getCities().add(c3);
//
// c1.setP(p);
// c2.setP(p);
// c3.setP(p);
// session.save(p);
//修改
//查询省份修改城市
// Province p = session.get(Province.class, 2);
// for(City c : p.getCities()){
// if(c.getCname().equals("湘潭")) {
// c.setCname("邵阳");
// }
// }
// session.save(p);
//得到城市修改省份
// City c = session.get(City.class, 1);
// c.setCname("湘潭");
// c.getP().setPname("湖北");
// session.save(c);
//删除
// 一断的 记得配置inverse="true" 否则删除失败 <set name="cities" table="city" cascade="all-delete-orphan" inverse="true">
// Province p = session.get(Province.class, 2);
// System.out.println(p);
// session.delete(p);
//提交
transaction.commit();
//关闭session及sessionFactory
session.close();
sessionFactory.close();
}
}
评论 (0)