一张表对应 一组对象
如员工信息 和工资休假 业务是分开 但可以存放在一张表里
- 子对象(员工工资休假对象)不具有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子标签指定属性与列名对应关系
<hibernate-mapping package="chen">
<class name="Staff" table="STAFF">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- 映射组成关系 -->
<component name="pay" class="Pay">
<!-- 指定组成关系属性 -->
<property name="annualLeave" column="annualLeave"></property>
<property name="pay" column="pay"></property>
</component>
</class>
</hibernate-mapping>
代码使用:
/**
* 映射组成关系
* 一个对象由多个对象组成的映射
*
*/
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子标签指定属性与列名对应关系
<hibernate-mapping package="chen">
<class name="Staff" table="STAFF">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- 映射组成关系 -->
<component name="pay" class="Pay">
<!-- 指定组成关系属性 -->
<property name="annualLeave" column="annualLeave"></property>
<property name="pay" column="pay"></property>
</component>
</class>
</hibernate-mapping>
代码使用:
/**
* 映射组成关系
* 一个对象由多个对象组成的映射
*
*/
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();
}
- 只是业务上分开 表还是一张
- 只是业务上分开 表还是一张