休眠组件表示为一组值或属性,而不是实体(表)。 请参阅以下教程,以了解组件如何在Hibernate中工作。
1.客户表
参见下面的客户表。
客户表,MySQL数据库中的SQL脚本。
CREATE TABLE `customer` (
`CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CUST_NAME` varchar(10) NOT NULL,
`AGE` int(10) unsigned NOT NULL,
`ADDRESS1` varchar(255) NOT NULL,
`ADDRESS2` varchar(255) NOT NULL,
`ADDRESS3` varchar(255) NOT NULL,
`CREATED_DATE` date NOT NULL,
`CREATED_BY` varchar(10) NOT NULL,
PRIMARY KEY (`CUST_ID`) USING BTREE
);
2.型号类别
现在,创建两个模型类Customer.java
和Address.java
,以表示上面的“ customer ”表。
文件:Customer.java
package com.mkyong.customer;
import java.util.Date;
public class Customer implements java.io.Serializable {
private Integer custId;
private String custName;
private int age;
private Address address;
private Date createdDate;
private String createdBy;
//setters and getters
}
文件:Address.java
package com.mkyong.customer;
public class Address implements java.io.Serializable {
private String address1;
private String address2;
private String address3;
//setters and getters
}
在这种情况下, Address.java
是“ 组件 ”,代表Customer.java
“ Address1”,“ Address2”和“ Address3”列
3.组件映射
现在,您可以像下面这样声明组件映射:
文件:Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mkyong.customer.Customer" table="customer"
catalog="mkyongdb">
<id name="custId" type="java.lang.Integer">
<column name="CUST_ID" />
<generator class="identity" />
</id>
<property name="custName" type="string">
<column name="CUST_NAME" length="10" not-null="true" />
</property>
<property name="age" type="int">
<column name="AGE" not-null="true" />
</property>
<component name="Address" class="com.mkyong.customer.Address">
<property name="address1" type="string">
<column name="ADDRESS1" not-null="true" />
</property>
<property name="address2" type="string">
<column name="ADDRESS2" not-null="true" />
</property>
<property name="address3" type="string">
<column name="ADDRESS3" not-null="true" />
</property>
</component>
<property name="createdDate" type="date">
<column name="CREATED_DATE" length="10" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
4.运行
创建地址对象,将其包含在客户对象中,然后将其持久化。
package com.mkyong;
import java.util.Date;
import org.hibernate.Session;
import com.mkyong.customer.Address;
import com.mkyong.customer.Customer;
import com.mkyong.util.HibernateUtil;
public class App {
public static void main(String[] args) {
System.out.println("Hibernate component mapping");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Address address = new Address();
address.setAddress1("Address 1");
address.setAddress2("Address 2");
address.setAddress3("Address 3");
Customer cust = new Customer();
cust.setCustName("mkyong");
cust.setAge(30);
cust.setAddress(address);
cust.setCreatedDate(new Date());
cust.setCreatedBy("system");
session.save(cust);
session.getTransaction().commit();
System.out.println("Done");
}
}
输出...
Hibernate component mapping
Hibernate:
insert
into
mkyongdb.customer
(CUST_NAME, AGE, ADDRESS1, ADDRESS2, ADDRESS3, CREATED_DATE, CREATED_BY)
values
(?, ?, ?, ?, ?, ?, ?)
Done
下载它– Hibernate-Component-Mapping-Example.zip (10KB)
参考
翻译自: https://mkyong.com/hibernate/hibernate-component-mapping-example/