***************
public void Map_Add() {
Session session = null;
Transaction tx = null;
try {
City city1 = new City();
city1.setName("中国·唐山");
city1.setPostcode("063009");
City city2 = new City();
city2.setName("中国·天津");
city2.setPostcode("356148");
Map<String, City> citys = new HashMap<String, City>();
citys.put("唐山", city1);
citys.put("天津", city2);
Nation nation = new Nation();
nation.setName("中国");
nation.setCitys(citys);
// 当Nation.hbm.xml文件中map节点的inverse = "false"或不写时(即默认,false)
// 这行代码的作用是Hibernate可以根据nation中的citys去更新city表中的CITYNAME
// 如果没有setCitys则city表中的CITYNAME为null
// 当Nation.hbm.xml文件中map节点的inverse = "true"时不起作用
city1.setNation(nation);
city2.setNation(nation);
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(nation);
session.save(city1);
session.save(city2);
tx.commit();
} catch (Exception ex) {
Logger.getLogger(Many2OneAndOne2Many.class.getName()).log(Level.SEVERE, null, ex);
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
public void Map_Add() {
Session session = null;
Transaction tx = null;
try {
City city1 = new City();
city1.setName("中国·唐山");
city1.setPostcode("063009");
City city2 = new City();
city2.setName("中国·天津");
city2.setPostcode("356148");
Map<String, City> citys = new HashMap<String, City>();
citys.put("唐山", city1);
citys.put("天津", city2);
Nation nation = new Nation();
nation.setName("中国");
nation.setCitys(citys);
// 当Nation.hbm.xml文件中map节点的inverse = "false"或不写时(即默认,false)
// 这行代码的作用是Hibernate可以根据nation中的citys去更新city表中的CITYNAME
// 如果没有setCitys则city表中的CITYNAME为null
// 当Nation.hbm.xml文件中map节点的inverse = "true"时不起作用
city1.setNation(nation);
city2.setNation(nation);
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(nation);
session.save(city1);
session.save(city2);
tx.commit();
} catch (Exception ex) {
Logger.getLogger(Many2OneAndOne2Many.class.getName()).log(Level.SEVERE, null, ex);
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
Hibernate打印的sql语句:
Hibernate: insert into nation (NATION_NAME) values (?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?
public void Map_Add() {
Session session = null;
Transaction tx = null;
try {
City city1 = new City();
city1.setName("中国·唐山");
city1.setPostcode("063009");
City city2 = new City();
city2.setName("中国·天津");
city2.setPostcode("356148");
Map<String, City> citys = new HashMap<String, City>();
citys.put("唐山", city1);
citys.put("天津", city2);
Nation nation = new Nation();
nation.setName("中国");
nation.setCitys(citys);
// 当Nation.hbm.xml文件中map节点的inverse = "false"或不写时(即默认,false)
// 这行代码的作用是Hibernate可以根据nation中的citys去更新city表中的CITYNAME
// 如果没有setCitys则city表中的CITYNAME为null
// 当Nation.hbm.xml文件中map节点的inverse = "true"时不起作用
city1.setNation(nation);
city2.setNation(nation);
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(city1);
session.save(city2);
session.save(nation);
tx.commit();
} catch (Exception ex) {
Logger.getLogger(Many2OneAndOne2Many.class.getName()).log(Level.SEVERE, null, ex);
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
Hibernate打印的sql语句:
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: insert into nation (NATION_NAME) values (?)
Hibernate: update city set CITY_NAME=?, POST_CODE=?, NATION_ID=? where CITY_ID=?
Hibernate: update city set CITY_NAME=?, POST_CODE=?, NATION_ID=? where CITY_ID=?
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?
以下的两条更新语句是由于先保存的City而引起的:
Hibernate: update city set CITY_NAME=?, POST_CODE=?, NATION_ID=? where CITY_ID=?
Hibernate: update city set CITY_NAME=?, POST_CODE=?, NATION_ID=? where CITY_ID=?
原因是由于City依赖于Nation的 NATION_ID,所以在保存City时如果没有保存Nation,则会在保存完Nation后更新City的 NATION_ID字段。所以建议将依赖的数据先保存(这里就是City依赖Nation),这样可以减少Hibernate对于数据库的操作,从而减少资源的消耗。