Hibernate一对多关联关系

关系表

create table provinces(
p_id varchar(32) primary key,
p_name varchar(40)
);
create table cities(
c_id varchar(32)primary key,
c_name varchar(40),
p_c_id varchar(32),
 foreign key(p_c_id)references provinces(p_id)
 
);

 

映射文件

<hibernate-mapping>
	<class name="pojo.Cities" table="cities" catalog="hibernate">
		<id name="CId" type="java.lang.String">
			<column name="c_id" length="32" />
			<generator class="uuid.hex" />
		</id>
		<many-to-one name="provinces" class="pojo.Provinces"
			fetch="select" cascade="none">
			<column name="p_c_id" length="32" />
		</many-to-one>
		<property name="CName" type="java.lang.String">
			<column name="c_name" length="40" />
		</property>
	</class>
	<class name="pojo.Provinces" table="provinces"
		catalog="hibernate">
		<id name="PId" type="java.lang.String">
			<column name="p_id" length="32" />
			<generator class="uuid.hex" />
		</id>
		<property name="PName" type="java.lang.String">
			<column name="p_name" length="40" />
		</property>
		<set name="citieses" inverse="true" cascade="all">
			<key>
				<column name="p_c_id" length="32" />
			</key>
			<one-to-many class="pojo.Cities" />
		</set>
	</class>
</hibernate-mapping>

 

CityDAO

package dao;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;

import config.HibernateSessionFactory;

import pojo.Cities;
import pojo.Provinces;

/*
 * ORMapping 一对多双向关联
 * */
public class CityDao {
    public void insertAll() {// 测试添加
	Provinces provinces = new Provinces();
	Cities cities1 = new Cities();
	Cities cities2 = new Cities();
	provinces.setPName("河南");
	provinces.getCitieses().add(cities1);
	provinces.getCitieses().add(cities2);
	cities1.setCName("郑州");
	cities2.setCName("开封");
	cities1.setProvinces(provinces);
	cities2.setProvinces(provinces);
	Session session = HibernateSessionFactory.getSession();
	session.beginTransaction();
	session.save(provinces);
	session.beginTransaction().commit();

    }

    public void insertCity() {// 测试单独添加
	Session session = HibernateSessionFactory.getSession();
	Cities cities = new Cities();
	String hql = "from Provinces where PName=? ";
	Query query = session.createQuery(hql);
	query.setString(0, "河南");
	Provinces provinces = (Provinces) query.uniqueResult();
	cities.setCName("洛阳");
	cities.setProvinces(provinces);
	provinces.getCitieses().add(cities);

	session.beginTransaction();
	session.save(provinces);
	session.beginTransaction().commit();
	session.close();
    }

    public void sel(String pName) {// 测试查询
	Session session = HibernateSessionFactory.getSession();
	Cities cities = new Cities();
	Provinces provinces = new Provinces();
	String hql = "from Provinces where PName=?";
	Query query = session.createQuery(hql);
	query.setString(0, pName);
	provinces = (Provinces) query.uniqueResult();
	System.out.println(provinces.getPName());
	System.out.println(provinces.getCitieses().size());
	Iterator iterator = provinces.getCitieses().iterator();
	while (iterator.hasNext()) {
	    cities = (Cities) iterator.next();

	    System.out.println(cities.getCName());
	}
    }

    public void update() {
	Session session = HibernateSessionFactory.getSession();
	/*
	 * 省份更改
	 * Cities cities = new Cities();

	Provinces provinces = new Provinces();
	String hql = "from Provinces where PName=?";
	Query query = session.createQuery(hql);
	query.setString(0, "台湾");
	provinces = (Provinces) query.uniqueResult();
	provinces.setPName("河南");
	session.beginTransaction();
	session.saveOrUpdate(provinces);
	session.beginTransaction().commit();
	session.close();
	*/ 
	Provinces provinces = new Provinces();//城市更改
	
	Query query = session.createQuery("from Provinces where PName=?");
	query.setString(0, "河南");
	provinces=(Provinces) query.list().get(0);
	Query q=session.createQuery("from Cities where CName=?");
	q.setString(0, "郑州");
	Cities cities=(Cities) q.list().get(0);
	System.out.println(cities.getCName());
	cities.setCName("南阳");
	cities.setProvinces(provinces);
	provinces.getCitieses().add(cities);
	session.beginTransaction();
	session.save(provinces);
	session.beginTransaction().commit();
	session.close();
	
    }
    public void delete(){
	Session session=HibernateSessionFactory.getSession();
	
	Query  query=session.createQuery("from Provinces p where p.PName=? ");
	query.setString(0, "河南");
	Provinces provinces=(Provinces) query.list().get(0);
	session.beginTransaction();
	session.delete(provinces);
	session.beginTransaction().commit();
	session.close();
    }

    public static void main(String[] args) {
	CityDao cityDao = new CityDao();
	// cityDao.insertAll();
	 cityDao.insertCity();
	 cityDao.sel("河南");
	//cityDao.update();
	//cityDao.delete();
    }

}

 

你可能感兴趣的:(DAO,C++,c,Hibernate,C#)