Hibernate06_配置文件详解
测试:
People people =new People();
people.setName("李四");
session.save(people);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
insert
into
people
(name, money)
values
(?, ?)
修改People.hbm.xml,设置 dynamic-insert="true"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.blu.entity.People" table="people" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="identity"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<property name="money" type="java.lang.Double">
<column name="money"></column>
</property>
</class>
</hibernate-mapping>
再次测试的结果:
Hibernate:
insert
into
people
(name)
values
(?)
测试:
People people = session.get(People.class, 2);
people.setMoney(2000.0);
session.update(people);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
select
people0_.id as id1_3_0_,
people0_.name as name2_3_0_,
people0_.money as money3_3_0_
from
people people0_
where
people0_.id=?
Hibernate:
update
people
set
name=?,
money=?
where
id=?
修改People.hbm.xml,设置 dynamic-update="true"
配置文件略
再次测试:
People people = session.get(People.class, 3);
people.setMoney(3000.0);
session.update(people);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
select
people0_.id as id1_3_0_,
people0_.name as name2_3_0_,
people0_.money as money3_3_0_
from
people people0_
where
people0_.id=?
Hibernate:
update
people
set
money=?
where
id=?
总结:使用 dynamic-insert=“true” 和 dynamic-update=“true” 后,SQL语句被优化,执行效率提高,提升了系统性能!
测试
String hql = "from People";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people : list) {
System.out.println(people);
}
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
select
people0_.id as id1_3_,
people0_.name as name2_3_,
people0_.money as money3_3_
from
people people0_
People(id=1, name=张三, money=1000.0)
People(id=2, name=李四, money=2000.0)
People(id=3, name=王五, money=3000.0)
修改People.hbm.xml,设置 where="id = 1"
配置文件略
再次测试的结果:
Hibernate:
select
people0_.id as id1_3_,
people0_.name as name2_3_,
people0_.money as money3_3_
from
people people0_
where
(
people0_.id = 1
)
People(id=1, name=张三, money=1000.0)
测试:
Customer customer = new Customer();
customer.setName("李四");
Orders order1 = new Orders();
order1.setName("订单a");
order1.setCustomer(customer);
Orders order2 = new Orders();
order2.setName("订单b");
order2.setCustomer(customer);
Set<Orders> orders = new HashSet<Orders>();
orders.add(order1);
orders.add(order2);
customer.setOrders(orders);
session.save(customer);
session.save(order1);
session.save(order2);
session.beginTransaction().commit();
session.close();
结果执行了5条SQL(如果先保存订单后保存客户,则会执行七条SQL):
Hibernate:
insert
into
customer
(name)
values
(?)
Hibernate:
insert
into
orders
(name, cid)
values
(?, ?)
Hibernate:
insert
into
orders
(name, cid)
values
(?, ?)
Hibernate:
update
orders
set
cid=?
where
id=?
Hibernate:
update
orders
set
cid=?
where
id=?
原因是Customer和Orders都在维护一对多关系,所以会重复设置主外键约束关系。
解决方法1:去掉一方维护关系的代码
Customer customer = new Customer();
customer.setName("李四");
Orders order1 = new Orders();
order1.setName("订单a");
order1.setCustomer(customer);
Orders order2 = new Orders();
order2.setName("订单b");
order2.setCustomer(customer);
session.save(customer);
session.save(order1);
session.save(order2);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
insert
into
customer
(name)
values
(?)
Hibernate:
insert
into
orders
(name, cid)
values
(?, ?)
Hibernate:
insert
into
orders
(name, cid)
values
(?, ?)
解决方法2:通过配置 inverse="true" 让Customer放弃关系维护权
<set name="orders" table="orders" lazy="extra" inverse="true">
<key column="cid"></key>
<one-to-many class="com.blu.entity.Orders"></one-to-many>
</set>
测试:
Customer customer = session.get(Customer.class, 10);
session.delete(customer);
session.beginTransaction().commit();
session.close();
删除报错,原因:外键约束
解决方法1:先删除所有的orders,再删除customer
Customer customer = session.get(Customer.class, 10);
Iterator<Orders> iterator = customer.getOrders().iterator();
while(iterator.hasNext()) {
session.delete(iterator.next());
}
session.delete(customer);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
select
customer0_.id as id1_1_0_,
customer0_.name as name2_1_0_
from
customer customer0_
where
customer0_.id=?
Hibernate:
select
orders0_.cid as cid3_2_0_,
orders0_.id as id1_2_0_,
orders0_.id as id1_2_1_,
orders0_.name as name2_2_1_,
orders0_.cid as cid3_2_1_
from
orders orders0_
where
orders0_.cid=?
Hibernate:
delete
from
orders
where
id=?
Hibernate:
delete
from
orders
where
id=?
Hibernate:
delete
from
customer
where
id=?
解决方法2: 通过配置 cascade="delete" 开启级联删除操作
<set name="orders" table="orders" lazy="extra" inverse="true" cascade="delete">
<key column="cid"></key>
<one-to-many class="com.blu.entity.Orders"></one-to-many>
</set>
测试:
Customer customer = session.get(Customer.class, 11);
session.delete(customer);
session.beginTransaction().commit();
session.close();
结果:
Hibernate:
select
customer0_.id as id1_1_0_,
customer0_.name as name2_1_0_
from
customer customer0_
where
customer0_.id=?
Hibernate:
select
orders0_.cid as cid3_2_0_,
orders0_.id as id1_2_0_,
orders0_.id as id1_2_1_,
orders0_.name as name2_2_1_,
orders0_.cid as cid3_2_1_
from
orders orders0_
where
orders0_.cid=?
Hibernate:
delete
from
orders
where
id=?
Hibernate:
delete
from
customer
where
id=?
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blucoding.blog.csdn.net/article/details/107811716
内容来源于网络,如有侵权,请联系作者删除!