組件映射
- 組建映射是指在一個實體類中有一個屬性為自定義的類,但是自定義的類中并沒有oid,在數據庫中也沒有對應這個類的表。
表結構:
/* 組件映射 */
CREATE TABLE t_customer(
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(20),
province VARCHAR2(20),
city VARCHAR2(20),
street VARCHAR2(20)
);
Customner類
package com.iotek.basic.component.pojo;
import java.io.Serializable;
public class Customer implements Serializable {
private static final long serialVersionUID = 5660598920023039135L;
private Long id;
private String name;
private Address address;
public Customer() {}
public Customer(Long id, String name, Address address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
組件類 Address.java
package com.iotek.basic.component.pojo;
import java.io.Serializable;
public class Address implements Serializable {
private static final long serialVersionUID = 1921592338080264702L;
private String province;
private String city;
private String street;
private Customer customer;
public Address() {}
public Address(String province, String city, String street, Customer customer) {
super();
this.province = province;
this.city = city;
this.street = street;
this.customer = customer;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + ", street=" + street + ", customer=" + customer
+ "]";
}
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iotek.basic.component.pojo">
<class name="Customer" table="T_CUSTOMER">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">t_customer_seq</param>
</generator>
</id>
<property name="name" type="string" column="NAME"/>
<component name="address" class="Address">
<parent name="customer"/>
<property name="province" />
<property name="city" />
<property name="street" />
</component>
</class>
</hibernate-mapping>
hibernate.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- property 元素用于配置Hibernate中的屬性 鍵:值 -->
<!-- hibernate.connection.driver_class : 連接數據庫的驅動 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- hibernate.connection.url : 連接數據庫的地址,路徑 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<!-- hibernate.connection.username : 連接數據庫的用戶名 -->
<property name="connection.username">guest</property>
<!-- hibernate.connection.password : 連接數據庫的密碼 -->
<property name="connection.password">guest</property>
<!-- show_sql: 操作數據庫時,會 向控制臺打印sql語句 -->
<property name="show_sql">true</property>
<!-- 數據庫方言配置 org.hibernate.dialect.Oracle10gDialect (選擇最短的)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 配置連接池最大連接數-->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 配置連接池最小連接數-->
<property name="hibernate.c3p0.min_size">2</property>
<!-- 配置連接池超時時間-->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 配置連接池最大數量-->!
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 配置連接池空閑檢索時間-->
<property name="hibernate.c3p0.idle_test_period">150</property>
<!-- 配置連接池自增數量-->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 配置連接池驗證-->
<property name="hibernate.c3p0.validate">false</property>
<mapping resource="com/iotek/basic/pojo/Student.hbm.xml"/>
<mapping resource="com/iotek/basic/inheritance/pojo/concrete.hbm.xml"/>
<mapping resource="com/iotek/basic/component/pojo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
看代碼就可以理解了。
總結:
1.png