一張表對(duì)應(yīng) 一組對(duì)象
如員工信息 和工資休假 業(yè)務(wù)是分開 但可以存放在一張表里
- 子對(duì)象(員工工資休假對(duì)象)不具有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;
}
}
- 主對(duì)象(員工基本信息)具有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標(biāo)簽配置 name屬性主對(duì)象對(duì)應(yīng)屬性名 class屬性是子對(duì)象的全類名
- component標(biāo)簽 使用property子標(biāo)簽指定屬性與列名對(duì)應(yīng)關(guān)系
<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>
<!-- 映射組成關(guān)系 -->
<component name="pay" class="Pay">
<!-- 指定組成關(guān)系屬性 -->
<property name="annualLeave" column="annualLeave"></property>
<property name="pay" column="pay"></property>
</component>
</class>
</hibernate-mapping>
代碼使用:
/**
* 映射組成關(guān)系
* 一個(gè)對(duì)象由多個(gè)對(duì)象組成的映射
*
*/
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();
}
數(shù)據(jù)庫(kù)
一張表對(duì)應(yīng) 一組對(duì)象
如員工信息 和工資休假 業(yè)務(wù)是分開 但可以存放在一張表里
- 子對(duì)象(員工工資休假對(duì)象)不具有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;
}
}
- 主對(duì)象(員工基本信息)具有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標(biāo)簽配置 name屬性主對(duì)象對(duì)應(yīng)屬性名 class屬性是子對(duì)象的全類名
- component標(biāo)簽 使用property子標(biāo)簽指定屬性與列名對(duì)應(yīng)關(guān)系
<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>
<!-- 映射組成關(guān)系 -->
<component name="pay" class="Pay">
<!-- 指定組成關(guān)系屬性 -->
<property name="annualLeave" column="annualLeave"></property>
<property name="pay" column="pay"></property>
</component>
</class>
</hibernate-mapping>
代碼使用:
/**
* 映射組成關(guān)系
* 一個(gè)對(duì)象由多個(gè)對(duì)象組成的映射
*
*/
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
- 只是業(yè)務(wù)上分開 表還是一張
- 只是業(yè)務(wù)上分開 表還是一張