码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-08-01 15:49:20      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

                                                     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();
}

 

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

标签:

原文地址:http://www.cnblogs.com/miller-zou/p/5724425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!