Hibernate学习7(映射组成关系)

一张表对应 一组对象

如员工信息 和工资休假 业务是分开 但可以存放在一张表里

  • 子对象(员工工资休假对象)不具有OID属性
package chen;

public class Pay {
    private int pay;//工资
    private int annualLeave;//假期
    
    public int getPay() {
        return pay;
    }
    public void setPay(int pay) {
        this.pay = pay;
    }
    public int getAnnualLeave() {
        return annualLeave;
    }
    public void setAnnualLeave(int annualLeave) {
        this.annualLeave = annualLeave;
    }
}

  • 主对象(员工基本信息)具有OID属性
package chen;

public class Staff {
    private Integer id;
    private String name;
    private Pay pay;//工资

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Pay getPay() {
        return pay;
    }

    public void setPay(Pay pay) {
        this.pay = pay;
    }
}
配置
  • component标签配置 name属性主对象对应属性名 class属性是子对象的全类名
  • component标签 使用property子标签指定属性与列名对应关系

    
        
            
            
        
        
            
        
        
        
            
            
            
        
    

代码使用:
    /**
     * 映射组成关系
     * 一个对象由多个对象组成的映射
     * 
     */
    public static void testRelation() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Staff staff = new Staff();
        staff.setName("张三");

        Pay pay = new Pay();
        pay.setAnnualLeave(5);
        pay.setPay(100000);
        staff.setPay(pay);
        session.save(staff);

        transaction.commit();
        session.close();
        sessionFactory.close();
    }
数据库
一张表对应 一组对象

如员工信息 和工资休假 业务是分开 但可以存放在一张表里

  • 子对象(员工工资休假对象)不具有OID属性
package chen;

public class Pay {
    private int pay;//工资
    private int annualLeave;//假期
    
    public int getPay() {
        return pay;
    }
    public void setPay(int pay) {
        this.pay = pay;
    }
    public int getAnnualLeave() {
        return annualLeave;
    }
    public void setAnnualLeave(int annualLeave) {
        this.annualLeave = annualLeave;
    }
}

  • 主对象(员工基本信息)具有OID属性
package chen;

public class Staff {
    private Integer id;
    private String name;
    private Pay pay;//工资

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Pay getPay() {
        return pay;
    }

    public void setPay(Pay pay) {
        this.pay = pay;
    }
}
配置
  • component标签配置 name属性主对象对应属性名 class属性是子对象的全类名
  • component标签 使用property子标签指定属性与列名对应关系

    
        
            
            
        
        
            
        
        
        
            
            
            
        
    

代码使用:
    /**
     * 映射组成关系
     * 一个对象由多个对象组成的映射
     * 
     */
    public static void testRelation() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Staff staff = new Staff();
        staff.setName("张三");

        Pay pay = new Pay();
        pay.setAnnualLeave(5);
        pay.setPay(100000);
        staff.setPay(pay);
        session.save(staff);

        transaction.commit();
        session.close();
        sessionFactory.close();
    }
image.png
  • 只是业务上分开 表还是一张
  • 只是业务上分开 表还是一张

你可能感兴趣的:(Hibernate学习7(映射组成关系))