Hibernate一对多双向注解配置实例

*customer和order是一对多的关系,一个用户可以有多条订单,一条订单只能从属于一个用户。
所以,customer是一的一方,order是多的一方。*

package com.lejuad.lejuadhome.manager.entity;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Generated;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

//一的一方
@Entity
@Table(name="customer",catalog="xltest")
public class Customer implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1347529317729031020L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="name")
    private String name;//用户名字

    @Column(name="cid")
    private int cid;//用于和order的orderNumber相关联的字段
    //注意:此处我们不用主键关联

    //一的一方的配置
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List orders;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getOrders() {
        return orders;
    }

    public void setOrders(List orders) {
        this.orders = orders;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

}
//多的一方
package com.lejuad.lejuadhome.manager.entity;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "orde",catalog="xltest")
public class Order implements Serializable{

    private static final long serialVersionUID = 5134744924238999336L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "orderNumber")
    private int orderNumber;

    @Column(name = "orderName")
    private String orderName;

    //order对象的orderNumber字段和customer对象的cid字段关联
    @ManyToOne(cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name="orderNumber", referencedColumnName="cid",insertable=false,updatable=false)
    private Customer customer;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getOrderNumber() {
        return orderNumber;
    }
    public void setOrderNumber(int orderNumber) {
        this.orderNumber = orderNumber;
    }



    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getOrderName() {
        return orderName;
    }
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", orderNumber=" + orderNumber
                + ", orderName=" + orderName + "]";
    }



}
//一个测试类,用来向这两个表级联添加数据

    CustomerService service1=(CustomerService) ThreadTest.cxt.getBean("customerService");
    System.out.println("======="+service1);
    for(int i=0;i<100;i++){
        Customer customer=new Customer();
        customer.setName("cus"+i);
        customer.setCid(i);//注意:这里一定要手动设置值

        List orders=new ArrayList();
        for(int j=0;j<10;j++){
            Order order=new Order();
            order.setOrderName("order"+j);
            order.setOrderName("order"+j);
            order.setOrderNumber(i);//注意:
            //这里设置的值和上面cid的值保持一致
            //因为cid和orderNumber是关联的字段,而且这两个字段不是主键不能自增
            //如果这两个关联的字段都是自增的,就可以不设置值
            orders.add(order);
        }

        customer.setOrders(orders);
        try {
            service1.saveData(customer);
            System.out.println("statics插入成功"+"****************"+i);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("statics插入失敗");
            }
        }
//根据给定的客户,查询该客户的所有订单
        String hql="from Customer c where c.id=1";
        Customer c=service1.findOneByHql(hql);
        System.out.println(c);

        List<Order> orders=c.getOrders();
        for(Order o:orders){
            System.out.println(o);
        }
        //或者
        /*List<Order> orders=c.getOrders();
        Iterator it=orders.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }*/
    //根据给定的订单,查出发出订单的客户
        Order o=new Order();
        o.setId(1);

        String hql="from Order as o where id="+o.getId();
        Order order=service.findOneByHql(hql);
        System.out.println(order);
        System.out.println(order.getCustomer());

        //并修改该客户的名字
        Customer customer=order.getCustomer();
        customer.setName("xiaolu");
        order.setCustomer(customer);
        System.out.println(order.getCustomer());
        service.update(order);

你可能感兴趣的:(Hibernate,spring,多对一双向,注解)