一.一对一(双向)
1.外键关联
两个类:User{uid,name,loginInfo},LoginInfo{lid,username,password,user}
两张表:t_user主表,t_logininfo从表
LoginInfo.hbm.xml文件
<hibernate-mapping package="com.tarena.demo.po">
<class name="LoginInfo" table="t_logininfo">
<id name="lid" column="lid">
<generator class="identity"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
<!--外键uid-->
<many-to-one
name="user"
class="User"
column="uid"
unique="true"
>
</many-to-one>
</class>
</hibernate-mapping>
User.hbm.xml文件
<hibernate-mapping package="com.tarena.demo.po">
<class name="User" table="t_user">
<id name="uid" column="uid">
<generator class="identity"></generator>
</id>
<property name="name"></property>
<one-to-one
name="loginInfo"
class="LoginInfo"
fetch="select"
cascade="all"
>
</one-to-one>
</class>
</hibernate-mapping>
测试类:
public class TestOne2One {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 级联添加
* 在<one-to-one>中添加cascade="all"
* cascade:级联,取值:all,save-update,delete
* 对象之间的关系需要手动实现
* 即user.setLoginInfo(info);
* info.setUser(user);
*/
@Test
public void test01Save(){
User user = new User();
user.setName("张三");
LoginInfo info = new LoginInfo();
info.setUsername("zs");
info.setPassword("123456");
user.setLoginInfo(info);
info.setUser(user);
Session session = HibernateUtils.getSession();
session.save(user);
session.beginTransaction().commit();
}
/**
* 运行结果:
* Hibernate: insert into t_user (name) values (?)
Hibernate: insert into t_logininfo (username, password, uid) values (?, ?, ?)
*/
/**
* 级联删除
* 理论可以实现,实际开发中不建议使用
*/
@Test
public void test02Delete(){
Session session = HibernateUtils.getSession();
User user = (User)session.get(User.class, 1);
session.delete(user);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 查询
* 查询user,同时对与user相关联的对象的获取方式:
* 抓取策略(关联对象获取方式):
* 默认使用联合查询的方式
* fetch="select":关联对象使用单独select语句查询
* fetch="join":关联对象使用左外连接查询
* 懒加载:load(),iterate()
lazy="proxy"
hibernate3.2之前的处理:对一对一的关联对象采用懒加载方式
hibernate3.5之前的处理:对一对一的关联对象懒加载失效
*/
@Test
public void test03GetUser(){
Session session = HibernateUtils.getSession();
User user = (User)session.load(User.class, 1);
System.out.println(user.getName());
//System.out.println(user.getLoginInfo().getUsername());
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select user0_.uid as uid0_0_, user0_.name as name0_0_ from t_user user0_ where user0_.uid=?
Hibernate: select logininfo0_.lid as lid1_0_, logininfo0_.username as username1_0_, logininfo0_.password
as password1_0_, logininfo0_.uid as uid1_0_ from t_logininfo logininfo0_ where logininfo0_.lid=?
张三
*/}
2.主键关联
两个类:User{uid,name,loginInfo},LoginInfo{lid,username,password,user}
两张表:t_user主表,t_logininfo从表
LoginInfo.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="LoginInfo" table="t_logininfo">
<id name="lid" column="lid">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="username"></property>
<property name="password"></property>
<one-to-one name="user" class="User"
constrained="true"/>
</class>
</hibernate-mapping>
User.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="User" table="t_user" >
<id name="uid" column="uid">
<generator class="identity"></generator>
</id>
<property name="name"></property>
<one-to-one
name="loginInfo" class="LoginInfo"
cascade="all"
fetch="select"
>
</one-to-one>
</class>
</hibernate-mapping>
测试类:
public class TestOne2One {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 级联添加
* 在<one-to-one>中添加cascade="all"
* cascade:级联,取值:all,save-update,delete
* 对象之间的关系需要手动实现
* 即user.setLoginInfo(info);
* info.setUser(user);
*/
@Test
public void test01Save(){
User user = new User();
user.setName("张三");
LoginInfo info = new LoginInfo();
info.setUsername("zs");
info.setPassword("123456");
user.setLoginInfo(info);
info.setUser(user);
Session session = HibernateUtils.getSession();
session.save(user);
session.beginTransaction().commit();
}
/**
* 运行结果:
* Hibernate: insert into t_user (name) values (?)
Hibernate: insert into t_logininfo (username, password, lid) values (?, ?, ?)
*/
/**
* 级联删除
* 理论可以实现,实际开发中不建议使用
*/
@Test
public void test02Delete(){
Session session = HibernateUtils.getSession();
User user = (User)session.get(User.class, 1);
session.delete(user);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select user0_.uid as uid0_0_, user0_.name as name0_0_ from t_user user0_ where user0_.uid=?
Hibernate: select logininfo0_.lid as lid1_0_, logininfo0_.username as username1_0_, logininfo0_.password as password1_0_ from t_logininfo logininfo0_ where logininfo0_.lid=?
Hibernate: delete from t_logininfo where lid=?
Hibernate: delete from t_user where uid=?
*/
/**
* 查询
* 查询user,同时对与user相关联的对象的获取方式:
* 抓取策略(关联对象获取方式):
* 默认使用联合查询的方式
* fetch="select":关联对象使用单独select语句查询
* fetch="join":关联对象使用左外连接查询
* 懒加载:load(),iterate()
lazy="proxy"
hibernate3.2之前的处理:对一对一的关联对象采用懒加载方式
hibernate3.5之前的处理:对一对一的关联对象懒加载失效
*/
@Test
public void test03GetUser(){
Session session = HibernateUtils.getSession();
User user = (User)session.load(User.class, 1);
System.out.println(user.getName());
//System.out.println(user.getLoginInfo().getUsername());
HibernateUtils.close(session);
}
/**
* 运行结果
* Hibernate: select user0_.uid as uid0_0_, user0_.name as name0_0_ from t_user user0_ where user0_.uid=?
Hibernate: select logininfo0_.lid as lid1_0_, logininfo0_.username as username1_0_, logininfo0_.password
as password1_0_ from t_logininfo logininfo0_ where logininfo0_.lid=?
张三
*/
/**
* 如果通过LoginInfo查找user.getName(),则采用懒加载方式
*/
@Test
public void test04GetLoginInfo(){
Session session = HibernateUtils.getSession();
LoginInfo loginInfo = (LoginInfo)session.load(LoginInfo.class, 1);
System.out.println(loginInfo.getUsername());
//System.out.println(loginInfo.getUser().getName());
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select logininfo0_.lid as lid1_0_, logininfo0_.username as username1_0_, logininfo0_.password
* as password1_0_ from t_logininfo logininfo0_ where logininfo0_.lid=?
zs
*/
}
二.一对多(双向)
两个类:Dept{did,dname, emps}(One方),Emp{eid,ename, dept}(Many方)
两张表:t_dept,t_emp()
Dept.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Dept" table="t_dept">
<id name="did" column="did">
<generator class="identity"></generator>
</id>
<property name="dname"></property>
<set name="emps" cascade="all" fetch="select" inverse="true">
<key column="did"></key>
<one-to-many class="Emp" />
</set>
</class>
</hibernate-mapping>
Emp.hm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Emp" table="t_emp" lazy="false">
<id name="eid" column="eid">
<generator class="identity"></generator>
</id>
<property name="ename"></property>
<many-to-one name="dept" column="did" class="Dept" lazy="proxy" fetch="join"></many-to-one>
</class>
</hibernate-mapping>
测试类:
public class TestOne2One {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 级联添加
* cascade:级联操作
* <set>元素中使用inverse:反转维护依赖关系的权力
*
*/
@Test
public void test01SaveDept(){
Dept dept = new Dept();
dept.setDname("研发部");
Set<Emp> emps = new HashSet<Emp>();
for(int i=1;i<=10;i++){
Emp emp = new Emp();
emp.setEname("员工"+i);
emp.setDept(dept);
emps.add(emp);
}
dept.setEmps(emps);
Session session = HibernateUtils.getSession();
session.save(dept);
session.beginTransaction().commit();
session.close();
}
/**
* 运行结果:
* Hibernate: insert into t_dept (dname) values (?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
如果将inverse设为false,则结果为
Hibernate: insert into t_dept (dname) values (?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
Hibernate: update t_emp set did=? where eid=?
*/
/**
* 测试查询Dept
* 关联集合支持懒加载
*/
@Test
public void test02GetDept(){
Session session = HibernateUtils.getSession();
Dept dept = (Dept)session.load(Dept.class, 1);
Set<Emp> emps = dept.getEmps();
// System.out.println(emps.size());
// for (Emp emp : emps) {
// System.out.println(emp.getEid()+":"+emp.getEname());
// }
System.out.println(dept.getDname());
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select dept0_.did as did0_0_, dept0_.dname as dname0_0_
* from t_dept dept0_ where dept0_.did=?
研发部
如果将System.out.println(emps.size());放出来,并且设lazy="extra",则结果为
Hibernate: select dept0_.did as did0_0_, dept0_.dname as dname0_0_
from t_dept dept0_ where dept0_.did=?
Hibernate: select count(eid) from t_emp where did =?
10
研发部
*/
/**
*测试查询Emp
*关联对象支持懒加载
*/
@Test
public void test02GetEmp(){
Session session = HibernateUtils.getSession();
Emp emp = (Emp)session.load(Emp.class, 1);
System.out.println(emp.getEname());
//System.out.println(emp.getDept().getDname());
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select dept0_.did as did0_0_, dept0_.dname as dname0_0_
* from t_dept dept0_ where dept0_.did=?
研发部
如果lazy="false",则结果为
Hibernate: select emp0_.eid as eid1_0_, emp0_.ename as ename1_0_, emp0_.did as did1_0_
from t_emp emp0_ where emp0_.eid=?
Hibernate: select dept0_.did as did0_0_, dept0_.dname as dname0_0_
from t_dept dept0_ where dept0_.did=?
员工1
*/
}
三.多对多
两个类:Goods{gid,gname,orders},Order{oid,title,goodses}
三张表:t_goods,t_order_goods,t_order
Goods.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Goods" table="t_goods">
<id name="gid" column="gid">
<generator class="identity"></generator>
</id>
<property name="gname"></property>
<set name="orders" table="t_order_goods" >
<key column="gid"></key>
<many-to-many class="Order" column="oid">
</many-to-many>
</set>
</class>
</hibernate-mapping>
Order.hbm,xml文件中
<hibernate-mapping package="com.tarena.demo.po" >
<class name="Order" table="t_order">
<id name="oid" column="oid">
<generator class="identity"></generator>
</id>
<property name="title"></property>
<set name="goodses" table="t_order_goods" cascade="all" >
<key column="oid"></key>
<many-to-many class="Goods" column="gid">
</many-to-many>
</set>
</class>
</hibernate-mapping>
测试文件:
public class TestOne2Many {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
@Test
public void test01SaveOrder(){
Order order = new Order();
order.setTitle("电子产品");
Set<Goods> goodses = new HashSet<Goods>();
for(int i=0;i<=3;i++){
Goods goods = new Goods();
goods.setGname("电子产品"+i);
goodses.add(goods);
}
order.setGoodses(goodses);
Session session = HibernateUtils.getSession();
session.save(order);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: insert into t_order (title) values (?)
Hibernate: insert into t_goods (gname) values (?)
Hibernate: insert into t_goods (gname) values (?)
Hibernate: insert into t_goods (gname) values (?)
Hibernate: insert into t_goods (gname) values (?)
Hibernate: insert into t_order_goods (oid, gid) values (?, ?)
Hibernate: insert into t_order_goods (oid, gid) values (?, ?)
Hibernate: insert into t_order_goods (oid, gid) values (?, ?)
Hibernate: insert into t_order_goods (oid, gid) values (?, ?)
*/
/**
* 添加订单,向订单中添加存在的商品
*
*/
@Test
public void test02SaveOrder(){
Order order = new Order();
order.setTitle("电子产品1");
Goods goods = new Goods();
goods.setGid(1);
Set<Goods> goodses = new HashSet<Goods>();
goodses.add(goods);
order.setGoodses(goodses);
Session session = HibernateUtils.getSession();
session.save(order);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: insert into t_order (title) values (?)
Hibernate: update t_goods set gname=? where gid=?
Hibernate: delete from t_order_goods where gid=?
Hibernate: insert into t_order_goods (oid, gid) values (?, ?)
这种结果出现的原因是因为在配置文件,cascade="all",当往订单中
添加goods表中已经存在的商品的时候,会把goods表中原来该商品信息
替换掉。然后在关联表中把原来拥有该商品的订单的那条记录删除掉,再
插入第二个订单与商品的那条记录。这种做法不可取。因此我们一般不用
cascade。
*/
/**
* 查询订单
*
*/
@Test
public void test03GetOrder(){
Session session = HibernateUtils.getSession();
Order order = (Order)session.get(Order.class, 1);
System.out.println(order.getTitle());
Set<Goods> goodses = order.getGoodses();
// for(Goods goods :goodses){
// System.out.println(goods.getGname());
// }
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select order0_.oid as oid2_0_, order0_.title as title2_0_
* from t_order order0_ where order0_.oid=?
电子产品
*/
}
上述test01SaveOrder方法运行后数据库的变化 上述test02SaveOrder运行后数据库的变化
四.继承映射(使用多表)
三个类:Book{author,publisher},Computer{cpu,memoary},Product{pid,name,price}
Book,Computer都继承于Product。
Project.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Product" table="t_product">
<id name="pid" >
<generator class="identity"></generator>
</id>
<property name="name"/>
<property name="price"/>
<joined-subclass name="Computer" table="t_computer">
<key column="id"></key>
<property name="cpu"></property>
<property name="memoary"></property>
</joined-subclass>
<joined-subclass name="Book" table="t_book">
<key column="id"></key>
<property name="author"></property>
<property name="publisher"></property>
</joined-subclass>
</class>
</hibernate-mapping>
测试类:
public class TestExtendsManyTable {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 添加Book,Project
*/
@Test
public void testSave(){
Book book = new Book();
book.setName("java");
book.setAuthor("Gos");
book.setPrice(100);
book.setPublisher("Sun");
Computer computer = new Computer();
computer.setName("惠普");
computer.setPrice(5000);
computer.setCpu("100核");
computer.setMemoary("1T");
Session session = HibernateUtils.getSession();
session.save(book);
session.save(computer);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: insert into t_product (name, price) values (?, ?)
Hibernate: insert into t_book (author, publisher, id) values (?, ?, ?)
Hibernate: insert into t_product (name, price) values (?, ?)
Hibernate: insert into t_computer (cpu, memoary, id) values (?, ?, ?)
*/
/**
* 删除Book
*/
@Test
public void testDelete(){
Session session = HibernateUtils.getSession();
Book book = (Book) session.load(Book.class, 1);
session.delete(book);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select book0_.id as pid0_0_, book0_1_.name as name0_0_, book0_1_.price as price0_0_,
* book0_.author as author2_0_, book0_.publisher as publisher2_0_ from t_book book0_
* inner join t_product book0_1_ on book0_.id=book0_1_.pid where book0_.id=?
Hibernate: delete from t_book where id=?
Hibernate: delete from t_product where pid=?
*/
/**
* 查询Book
*/
@Test
public void testLoad(){
Session session = HibernateUtils.getSession();
Book book = (Book) session.load(Book.class, 1);
System.out.println(book.getName()+","+book.getPublisher());
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select book0_.id as pid0_0_, book0_1_.name as name0_0_, book0_1_.price as price0_0_, book0_.author
* as author2_0_, book0_.publisher as publisher2_0_ from t_book book0_ inner join t_product book0_1_ on book0_.id=book0_1_.pid
* where book0_.id=?
java,Sun
*/
/**
* 查询Product
*/
@Test
public void testLoadProduct(){
Session session = HibernateUtils.getSession();
List<Product> products = session.createQuery("from Product").list();
for (Product product : products) {
if(product instanceof Computer){
Computer computer = (Computer)product;
System.out.println(computer.getCpu()+","+computer.getMemoary());
}else if(product instanceof Book){
Book book = (Book)product;
System.out.println(book.getName()+","+book.getPublisher());
}
}
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: select product0_.pid as pid0_, product0_.name as name0_, product0_.price as price0_,
* product0_1_.cpu as cpu1_, product0_1_.memoary as memoary1_, product0_2_.author as author2_,
* product0_2_.publisher as publisher2_, case when product0_1_.id is not null then 1
* when product0_2_.id is not null then 2 when product0_.pid is not null then 0 end as clazz_
* from t_product product0_ left outer join t_computer product0_1_ on product0_.pid=product0_1_.id
* left outer join t_book product0_2_ on product0_.pid=product0_2_.id
java,Sun
100核,1T
*/
}
五.继承映射(使用单表)
三个类:Book{author,publisher},Computer{cpu,memoary},Project{pid,name,price}
Book,Computer都继承于Product。
Project.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Product" table="t_product">
<id name="pid" >
<generator class="identity"></generator>
</id>
<!-- 生成区分字段 -->
<discriminator column="p_type"></discriminator>
<property name="name"/>
<property name="price"/>
<!-- 设置该类型在区分字段中的值 -->
<subclass name="Book" discriminator-value="B">
<property name="author"/>
<property name="publisher"/>
</subclass>
<subclass name="Computer" discriminator-value="C">
<property name="cpu"/>
<property name="memoary"/>
</subclass>
</class>
</hibernate-mapping>
测试类:
public class TestExtendsManyTable {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**-
* 添加Book,Project
*/
@Test
public void testSave(){
Book book = new Book();
book.setName("java");
book.setAuthor("Gos");
book.setPrice(100);
book.setPublisher("Sun");
Computer computer = new Computer();
computer.setName("惠普");
computer.setPrice(5000);
computer.setCpu("100核");
computer.setMemoary("1T");
Session session = HibernateUtils.getSession();
session.save(book);
session.save(computer);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: insert into t_product (name, price, author, publisher, p_type) values (?, ?, ?, ?, 'B')
Hibernate: insert into t_product (name, price, cpu, memoary, p_type) values (?, ?, ?, ?, 'C')
*/
/**
* 删除Book
*/
@Test
public void testDelete(){
Session session = HibernateUtils.getSession();
Book book = (Book) session.load(Book.class, 1);
session.delete(book);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: select book0_.pid as pid0_0_, book0_.name as name0_0_, book0_.price as price0_0_,
* book0_.author as author0_0_, book0_.publisher as publisher0_0_ from t_product book0_
* where book0_.pid=? and book0_.p_type='B'
Hibernate: delete from t_product where pid=?
*/
/**
* 查询Book
*/
@Test
public void testLoad(){
Session session = HibernateUtils.getSession();
Book book = (Book) session.load(Book.class, 1);
System.out.println(book.getName()+","+book.getPublisher());
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: select book0_.pid as pid0_0_, book0_.name as name0_0_, book0_.price as price0_0_,
* book0_.author as author0_0_, book0_.publisher as publisher0_0_ from t_product book0_
* where book0_.pid=? and book0_.p_type='B'
java,Sun
*/
/**
* 查询Product
*/
@Test
public void testLoadProduct(){
Session session = HibernateUtils.getSession();
List<Product> products = session.createQuery("from Product").list();
for (Product product : products) {
if(product instanceof Computer){
Computer computer = (Computer)product;
System.out.println(computer.getCpu()+","+computer.getMemoary());
}else if(product instanceof Book){
Book book = (Book)product;
System.out.println(book.getName()+","+book.getPublisher());
}
}
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: select product0_.pid as pid0_, product0_.name as name0_,
* product0_.price as price0_, product0_.author as author0_,
* product0_.publisher as publisher0_, product0_.cpu as cpu0_,
* product0_.memoary as memoary0_, product0_.p_type as p2_0_ from t_product product0_
java,Sun
100核,1T
*/
}
六.一对多中使用list集合映射
两个类:Dept{did,dname,emps},Emp{dept,eid,ename}
Dept.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Dept" table="t_dept">
<id name="did" column="did">
<generator class="identity"></generator>
</id>
<property name="dname"></property>
<!--
<set name="emps" cascade="all" inverse="true">
<key column="did"></key>
<one-to-many class="Emp"/>
</set>
-->
<!-- 使用list集合 -->
<list name="emps" cascade="all">
<key column="did"></key>
<list-index column="t_index" base="0"/>
<one-to-many class="Emp"/>
</list>
</class>
</hibernate-mapping>
Emp.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Emp" table="t_emp">
<id name="eid" column="eid">
<generator class="identity"></generator>
</id>
<property name="ename"></property>
<many-to-one name="dept" column="did" class="Dept"></many-to-one>
</class>
</hibernate-mapping>
测试类:
public class TestOne2Many {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 级联添加
* cascade:级联操作
* <list>元素中使用
* 使用list映射,inverse="true"不能加。如果加上的话,save的时候,全是
* insert语句,没有update语句,性能是提高了。但是表中的t_index中全部是空值,
* 如果查询dept,在dept.getEmps(),得到其中的emps迭代是查询不到的,
* 因此,如果要用list,就不要加inverse属性
*/
@Test
public void test01SaveDept(){
Dept dept = new Dept();
dept.setDname("研发部");
List<Emp> emps = new ArrayList<Emp>();
for(int i=1;i<=10;i++){
Emp emp = new Emp();
emp.setEname("员工"+i);
emp.setDept(dept);
emps.add(emp);
}
dept.setEmps(emps);
Session session = HibernateUtils.getSession();
session.save(dept);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: insert into t_dept (dname) values (?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: insert into t_emp (ename, did) values (?, ?)
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
Hibernate: update t_emp set did=?, t_index=? where eid=?
*/
@Test
public void test02LoadDept(){
Session session = HibernateUtils.getSession();
Dept dept = (Dept) session.load(Dept.class, 1);
System.out.println(dept.getDname());
List<Emp> emps = dept.getEmps();
for (Emp emp : emps) {
System.out.println(emp.getEname());
}
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 测试结果:
* Hibernate: select dept0_.did as did0_0_, dept0_.dname as dname0_0_ from t_dept dept0_ where dept0_.did=?
研发部
Hibernate: select emps0_.did as did0_1_, emps0_.eid as eid1_, emps0_.t_index as t4_1_, emps0_.eid as eid1_0_,
emps0_.ename as ename1_0_, emps0_.did as did1_0_ from t_emp emps0_ where emps0_.did=?
员工1
员工2
员工3
员工4
员工5
员工6
员工7
员工8
员工9
员工10
*/
}
七.组件映射
组件映射:
减少代码的重复,提高面向对象
具体映射使用
两个类:Address{city,street},Person{address,id,name}
Person.hbm.xml文件中
<hibernate-mapping package="com.tarena.demo.po">
<class name="Person" table="t_person">
<id name="id" column="pid">
<generator class="identity"></generator>
</id>
<property name="name"></property>
<!-- 组件映射 -->
<component name="address">
<property name="street"/>
<property name="city"/>
</component>
</class>
</hibernate-mapping>
测试类:
public class TestComponent {
/**
* Hibernate生成关系模型
*/
@Test
public void test00DDL(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
/**
* 添加person
*/
@Test
public void test01SavePerson(){
Person person = new Person();
person.setName("张三");
Address address = new Address();
address.setStreet("华人街");
address.setCity("华盛顿");
person.setAddress(address);
Session session = HibernateUtils.getSession();
session.save(person);
session.beginTransaction().commit();
HibernateUtils.close(session);
}
/**
* 运行结果:
* Hibernate: insert into t_person (name, street, city) values (?, ?, ?)
*/
}