n21:
1.new 两个实体类,一个代表"多"的一端,一个代表"一"的一端.
Customer类:
public class Customer { private Integer customerId;
private String customerName;
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
} }
Order类:
public class Order { private Integer orderId;
private String orderName; private Customer customer; public Integer getOrderId() {
return orderId;
} public void setOrderId(Integer orderId) {
this.orderId = orderId;
} public String getOrderName() {
return orderName;
} public void setOrderName(String orderName) {
this.orderName = orderName;
} public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} }
2.生成关于这两个类的xml文件:
Order.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"> <hibernate-mapping package="n21"> <!-- 映射类和数据表 -->
<class name="Order" table="ORDERS">
<!-- 映射OID和数据表的主键 -->
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID"/>
<!-- 指定主键的生成方式 -->
<generator class="native"/>
</id> <!-- 映射属性和字段 -->
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME"/>
</property> <!--
映射多对一的关联关系.使用many-to-one来映射多对一的关联关系
name:多这一端关联的一那一端的属性的名字
class:多这一端的属性对应一那一端的类名
column:一那一端在多的一端对应的数据表中的外键的名字
-->
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one>
</class>
</hibernate-mapping>
Customer.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"> <hibernate-mapping package="n21"> <!-- 映射类和数据表 -->
<class name="Customer" table="CUSTOMERS">
<!-- 映射OID和数据表的主键 -->
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID"/>
<!-- 指定主键的生成方式 -->
<generator class="native"/>
</id> <!-- 映射属性和字段 -->
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME"/>
</property>
</class>
</hibernate-mapping>
3.hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory> <!-- hibernate的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate</property> <!-- hibernate的基本配置 --> <!-- hibernate使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 运行时是否打印sql语句 -->
<property name="show_sql">true</property> <!-- 运行时是否格式化sql -->
<property name="format_sql">true</property> <!-- 生成数据表的策略 这里是更新-->
<property name="hbm2ddl.auto">update</property> <!-- 设置hibernate的事务隔离级别 -->
<property name="connection.isolation">2</property> <!-- 需要关联的hibernate映射文件 .hbm.xml -->
<!--
<mapping resource="n21/Customer.hbm.xml"/>
<mapping resource="n21/Order.hbm.xml"/>
-->
<mapping resource="n21/both/Customer.hbm.xml"/>
<mapping resource="n21/both/Order.hbm.xml"/> </session-factory>
</hibernate-configuration>
4.Hibernate测试类:
package n21; import org.hibernate.LazyInitializationException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class HibernateTest { //生产环境中Session和Transaction是不能作为成员变量的,因为可能有并发的问题
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init(){
//1.创建一个SessionFactory对象
//1).创建Configuration对象:对应hibernate的基本配置信息和对象关系映射信息
Configuration configuration=new Configuration().configure();
//2).创建一个ServiceRegistry对象:hibernate 4.x新添加的对象
//hibernate的任何配置和服务都需要在该对象中注册后才能有效.
ServiceRegistry serviceRegistry=
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//3).获得到SessionFactory对象
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session对象
session=sessionFactory.openSession();
//3.开启事务
transaction=session.beginTransaction();
} @After
public void destroy(){
//5.提交事务
transaction.commit();
//6.关闭Session
session.close();
//7.关闭SessionFactory对象
sessionFactory.close();
} @Test
public void testDelete(){
//在不设定级联关系的情况下,且1这一端的对象有n那一端的对象在引用,不能直接删除1这一端的对象
Customer customer=(Customer) session.get(Customer.class, 1);
session.delete(customer);
} @Test
public void testUpdate(){
Order order=(Order) session.get(Order.class, 1);
order.getCustomer().setCustomerName("AAA");
} @Test
public void testManyToOneGet(){
//1.若查询多的一端的一个对象,则默认情况下,只查询了多的一端的对象,而没有查询关联的
//一的那一端的对象!--(延迟加载)?
Order order=(Order) session.get(Order.class, 1);
System.out.println(order.getOrderName()); System.out.println(order.getCustomer().getClass().getName()); //session.close(); //2.在需要使用到关联的对象时,才发送对应的SQL语句. 也叫懒加载
Customer customer=order.getCustomer();
System.out.println(customer.getCustomerName()); //3.在查询Customer对象时,由多的一端导航到1的一端时,
//若此时session已经被关闭,则默认情况下
//会发生LazyInitializationException 异常 //4.获取Order对象时,默认情况下,其关联的Customer对象是一个代理对象!
} @Test
public void testManyToOneSave(){
Customer customer=new Customer();
customer.setCustomerName("AA"); Order order1=new Order();
order1.setOrderName("ORDER-1"); Order order2=new Order();
order2.setOrderName("ORDER-2"); //设置关联关系
order1.setCustomer(customer);
order2.setCustomer(customer); //执行save操作:先插入Customer,再插入Order.3条INSERT
//先插入1的一端,再插入n的一端,只有INSERT语句
session.save(customer);
session.save(order1);
session.save(order2); //先插入Order,再插入Customer.3条INSERT,2条UPDATE
//先插入n的一端,再插入1的一端,会多出UPDATE语句!
//原因:因为在插入多的一端时,无法确定1的那端的外键值.所以只能等1的那端插入后,再额外发送UPDATE语句.
//推荐先插入1的一端,后插入n的一端
// session.save(order1);
// session.save(order2);
//
// session.save(customer);
}
}