Hibernate学习---第五节:普通组件和动态组件

时间:2021-11-25 02:53:53

一、普通组件映射配置

1、创建组件类,代码如下:

package learn.hibernate.bean;

/**
* 组件类
*/
public class Phones { private String companyPhone;
private String homePhone;
private String personalPhone; public Phones() { } public Phones(String companyPhone, String homePhone, String personalPhone) {
super();
this.companyPhone = companyPhone;
this.homePhone = homePhone;
this.personalPhone = personalPhone;
} @Override
public String toString() {
return "Phones [companyPhone=" + companyPhone + ", homePhone="
+ homePhone + ", personalPhone=" + personalPhone + "]";
} public String getCompanyPhone() {
return companyPhone;
}
public void setCompanyPhone(String companyPhone) {
this.companyPhone = companyPhone;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getPersonalPhone() {
return personalPhone;
}
public void setPersonalPhone(String personalPhone) {
this.personalPhone = personalPhone;
}
}
package learn.hibernate.bean;

/**
* 组件类
*/
public class Address { private String zipCode;
private String address; public Address() {
super();
} public Address(String zipCode, String address) {
super();
this.zipCode = zipCode;
this.address = address;
} @Override
public String toString() {
return "Address [zipCode=" + zipCode + ", address=" + address + "]";
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

2、将组件类作为属性放入到主类中,代码如下:

package learn.hibernate.bean;

import java.util.Date;

/**
* 持久化类
*/
public class Person { private Integer id;
private String name;
private int age;
private int passwork;
private Date birthday;
// 组件实例
private Address addres;
// 组件实例
private Phones phone; public Person() { } public Person(String name, int age, int passwork, Date birthday) {
super();
this.name = name;
this.age = age;
this.passwork = passwork;
this.birthday = birthday;
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", passwork=" + passwork + ", birthday=" + birthday + "]";
} 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPasswork() {
return passwork;
}
public void setPasswork(int passwork) {
this.passwork = passwork;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
} public Address getAddres() {
return addres;
} public void setAddres(Address addres) {
this.addres = addres;
} public Phones getPhone() {
return phone;
} public void setPhone(Phones phone) {
this.phone = phone;
} }

3、配置文件,代码如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="learn.hibernate.bean">
<class name="Person" table="t_person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name" column="t_name"/>
<property name="age"/>
<property name="passwork"/>
<property name="birthday"/>
<!--
组件类的映射配置
component 指定需要映射的组件类
name 指定 Person 中组件属性的变量名称
name的值"addres" 与 Person 中定义的要一致
-->
<component name="addres">
<property name="zipCode"/>
<property name="address"/>
</component> <component name="phone">
<property name="companyPhone"/>
<property name="homePhone"/>
<property name="personalPhone"/>
</component>
</class>
</hibernate-mapping>

4、测试代码:

@Test
public void testComponent() {
Person p = new Person("sdf",23,123456,new Date());
Address address = new Address("410000","湖南长沙");
Phones phone = new Phones("07318678987","0731876567","15114565678");
// person 与 address 关联
p.setAddres(address);
// person 与 phone 关联
p.setPhone(phone); tx = session.beginTransaction(); session.persist(p); tx.commit(); }

二、动态组件映射配置

1、创建类,代码如下:

package learn.hibernate.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map; /**
* 持久化类设计
* 注意:
* 持久化类通常建议要有一个持久化标识符(ID)
* 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
* 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
* 属性通常建议提供 getter/setter 方法
* 持久化类不能使用 final 修饰
* 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
* 如下:ArrayList list = new ArrayList(); 不行
* List list = new ArrayList(); 可行
*/
public class Person { private Integer id;
private String name;
private int age;
private int passwork;
private Date birthday;
// 动态组件实例
private Map attribute = new HashMap(); public Person() { } public Person(String name, int age, int passwork, Date birthday) {
super();
this.name = name;
this.age = age;
this.passwork = passwork;
this.birthday = birthday;
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", passwork=" + passwork + ", birthday=" + birthday + "]";
} 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPasswork() {
return passwork;
}
public void setPasswork(int passwork) {
this.passwork = passwork;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
} public Map getAttribute() {
return attribute;
} public void setAttribute(Map attribute) {
this.attribute = attribute;
} }

2、映射配置文件,代码如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="learn.hibernate.bean">
<class name="Person" table="t_person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name" column="t_name"/>
<property name="age"/>
<property name="passwork"/>
<property name="birthday"/>
<!--
动态组件类的映射配置
dynamic-component 指定需要映射的组件类
name="attribute" 对应持久化类中集合的变量名称
property Map 集合中key映射配置
name 对 Map 集合的key
column 存储 key 所对应的值
type 字段的数据类型
-->
<dynamic-component name="attribute">
<property name="key1" column="t_key1" type="string"/>
<property name="key2" column="t_key1" type="integer"/>
</dynamic-component>
</class>
</hibernate-mapping>

3、测试代码:

@Test
public void testComponent() {
Person p = new Person("sdf",23,123456,new Date());
// 在 Person 中只有声明,也有创建
Map attribute = p.getAttribute();
attribute.put("key1", "hibernate");
attribute.put("key2", 123); tx = session.beginTransaction(); session.persist(p); tx.commit(); }

4、如果在 Person 类中只声明了动态组件,并未创建,如下:

package learn.hibernate.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map; /**
* 持久化类设计
* 注意:
* 持久化类通常建议要有一个持久化标识符(ID)
* 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
* 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
* 属性通常建议提供 getter/setter 方法
* 持久化类不能使用 final 修饰
* 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
* 如下:ArrayList list = new ArrayList(); 不行
* List list = new ArrayList(); 可行
*/
public class Person { private Integer id;
private String name;
private int age;
private int passwork;
private Date birthday;
// 动态组件实例
private Map attribute; public Person() { } public Person(String name, int age, int passwork, Date birthday) {
super();
this.name = name;
this.age = age;
this.passwork = passwork;
this.birthday = birthday;
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", passwork=" + passwork + ", birthday=" + birthday + "]";
} 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPasswork() {
return passwork;
}
public void setPasswork(int passwork) {
this.passwork = passwork;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
} public Map getAttribute() {
return attribute;
} public void setAttribute(Map attribute) {
this.attribute = attribute;
} }

5、那么在测试代码需要创建一个,代码如下:

@Test
public void testAttribute() {
Person p = new Person("sdf",23,123456,new Date());
// 在 Person 中只有声明,没有创建
Map attribute = new HashMap();
attribute.put("key1", "hibernate");
attribute.put("key2", 123);
p.setAttribute(attribute); tx = session.beginTransaction(); session.persist(p); tx.commit(); }

6、查询,测试代码:

@Test
public void testGetAttribute() {
Person p = (Person)session.get(Person.class, 1);
System.out.println(p); // 高效操作 map 集合
Iterator<Map.Entry> it = p.getAttribute().entrySet().iterator(); for(;it.hasNext();){
Map.Entry map = it.next();
System.out.println(map.getKey()+"---------"+map.getValue());
}
}