Hibernate学习8(映射多对一的关联关系(外键))

映射多对一的关联关系用的是数据库的外键

如一个用户可以有多个订单

一的一端对象(用户):
package chen;
//顾客
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;
    }
}

多的一端对象(订单)
package chen;
//订单
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;
    }
}

配置文件
  • 一的一端映射文件:

    
        
            
            
        
        
            
        
    

  • 多的一端映射文件:

    
        
            
            
        
        
            
        
        
        
        
    

映射多对一在多端配置
使用many-to-one标签来 映射多对一的关联关系 name属性指向多端对象属性名 class一端对象全类名 column属性对应数据库列名
订单表
顾客表
保存到数据库
  • 先保存一的对象到数据库 在保存多的对象到数据库 这样是最优化不会多出update语句
    /**
     * 映射多对一的关联关系(外键)保存
     * */
    public static void testForeignKey() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Customer customer=new Customer();
        customer.setCustomerName("张三");
        Order order1=new Order();
        order1.setOrderName("自行车");
        order1.setCustomer(customer);
        Order order2=new Order();
        order2.setOrderName("帐篷");
        order2.setCustomer(customer);
        
        session.save(customer);
        session.save(order1);
        session.save(order2);
        //外键值没有先保存得到外键值的化会多出update语句
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
查询
  • 1.默认不会查询外键的关联对象
  • 2.在要使用外键关联的对象时才会查询(懒加载)
  • 3.会发生懒加载移除
  • 4.默认情况下外键指向的对象是一个代理对象
    /**
     * 映射多对一的关联关系(外键)查询
     * 
     * 1.默认不会查询外键的关联对象
     * 2.在要使用外键关联的对象时才会查询(懒加载)
     * 3.会发生懒加载移除
     * 4.默认情况下外键指向的对象是一个代理对象
     * */
    public static void testForeignKeyGet() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
        Order order = session.get(Order.class, 1);
        Customer customer = order.getCustomer();
        System.out.println(customer);
        
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
更新 和正常使用一样
/**
     * 映射多对一的关联关系(外键)更新
     * */
    public static void testForeignKeyUpdate() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Order order = session.get(Order.class, 1);
        order.getCustomer().setCustomerName("王五");
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
删除
  • 1 如果没有设置级联,没有被其他表数据外键所应用才可以删除
/**
     * 映射多对一的关联关系(外键)删除
     * 1 如果没有设置级联,没有被其他表数据外键所应用才可以删除
     * */
    public static void testForeignKeyDelete() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
        Customer customer = session.get(Customer.class, 1);
        session.delete(customer);
        
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

你可能感兴趣的:(Hibernate学习8(映射多对一的关联关系(外键)))