Hibernate5.2之单向一对多(二)

一. 简介

  在本篇博文中笔者会在上一篇博客的代码基础上进行修改,本篇文章将介绍单向的一对多。

二. hbm文件的方式

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.demo.hibernate.one2many.Customer" table="customer">
<id name="id" type="int">
<generator class="sequence">
<param name="sequence_name">cus_order_seq</param>
</generator>
</id>
<property name="name" type="string" column="name"></property>
<property name="phoneNum" type="string" column="phone_number"></property> </class>
</hibernate-mapping>

Order.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.demo.hibernate.one2many.Order" table="orders">
<id name="id" type="int">
<generator class="sequence">
<param name="sequence_name">cus_order_seq</param>
</generator>
</id>
<property name="orderId" column="order_id" type="string"></property>
<property name="createTime" column="create_time" type="timestamp"></property>
<many-to-one name="customer" class="com.demo.hibernate.one2many.Customer" column="customer_id"></many-to-one>
</class>
</hibernate-mapping>

三. 注解的方式

Customer.java

@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="id")
@SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1)
@GeneratedValue(generator="sequenceGenerator")
private int id; @Column(name="name")
private String name; @Column(name="phone_number")
private String phoneNum; //setter and getter
}

Order.java

@Entity
@Table(name="orders")
public class Order {
@Id
@Column(name="id")
@SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1)
@GeneratedValue(generator="sequenceGenerator")
private int id; @Column(name="order_id")
private String orderId; @Column(name="create_time")
@Type(type="timestamp")
private Date createTime; @ManyToOne(fetch=FetchType.LAZY) //注解的方式需要指定延迟加载策略
@JoinColumn(name="customer_id")
private Customer customer; //setter and getter
}

四.测试代码

A.保存

@Test
public void save(){
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setName("AAAAA");
customer.setPhoneNum("334411"); Order order = new Order();
order.setCreateTime(new Date());
order.setOrderId("A");
order.setCustomer(customer); Order order1 = new Order();
order1.setCreateTime(new Date());
order1.setOrderId("B");
order1.setCustomer(customer); session.save(customer);
session.save(order);
session.save(order1);
tx.commit();
}

B.get方式

@Test
public void get(){
Order order = session.get(Order.class, 17);
System.out.println("查询Customer的SQL已经发送");
System.out.println(order.getOrderId() + "::" + order.getCreateTime());
Customer customer = order.getCustomer();
System.out.println("查询Customer的SQL还未发送,因为延迟加载,只有当在使用Customer的时候才会发送SQL");
System.out.println(customer.getName() + "::" + customer.getPhoneNum());
}

C.load方式

@Test
public void load(){
Order order = session.load(Order.class, 17);
System.out.println("查询Order的SQL还未发送,只有当使用的时候才会放松SQL");
System.out.println(order.getOrderId() + "::" + order.getCreateTime());
Customer customer = order.getCustomer();
System.out.println("查询Order的Customer还未发送,因为延迟加载,只有当在使用Customer的时候才会发送SQL");
System.out.println(customer.getName() + "::" + customer.getPhoneNum());
}

D.delete(删除)

@Test
public void delete(){
Order order = new Order();
order.setId(17);
Transaction tx = session.beginTransaction();
session.delete(order);
tx.commit();
}

E.update(更新)

@Test
public void update(){
Order order = new Order();
order.setId(18);
order.setCreateTime(new Date());
order.setOrderId("N"); Transaction tx = session.beginTransaction();
session.update(order);
tx.commit();
}
05-11 17:25