三、实体层设计
----
细粒度划分
--Hibernate--
组件映射
java
代码
1.
Person.java
2.
3.
/*
4.
* Hibernate -
组件(
Component
)映射
5.
*
创建日期
2005-4-10
6.
* @author javamxj
(分享
java
快乐)
7.
* @link Blog: htpp://javamxj.mblogger.cn
8.
* htpp://blog.csdn.net/javamxj/
9.
*/
10.
package
javamxj.hibernate.component;
11.
/**
12.
* @hibernate.class
13.
*/
14.
public
class
Person {
15.
private
Long id;
16.
private
String username;
17.
private
Address address;
18.
/**
19.
* @hibernate.id
20.
* generator-class="hilo"
21.
* unsaved-value="null"
22.
*/
23.
public
Long getId() {
return
id;}
24.
public
void
setId(Long id) {
this
.id = id;}
25.
/**
26.
* @hibernate.property
27.
* length="15"
28.
* unique="true"
29.
* not-null="true"
30.
*/
31.
public
String getUsername() {
return
username;}
32.
public
void
setUsername(String username) {
this
.username = username;}
33.
/**
34.
* @hibernate.component
35.
*/
36.
public
Address getAddress() {
return
address;}
37.
public
void
setAddress(Address address) {
this
.address = address;}
38.
}
39.
· Person
类调用了
Address
类,注意在
“getAddress()”
方法上的
“ @hibernate.component”
标记。
· Address
类只含有一些
“ @hibernate.property”
标记,没有将其独立映射为一个表。
java
代码
1.
Address.java
2.
3.
package
javamxj.hibernate.component;
4.
public
class
Address {
5.
private
String country;
6.
private
String city;
7.
private
String street;
8.
private
String zipCode;
9.
public
Address() {}
10.
public
Address(String country, String city, String street, String zipcode) {
11.
super
();
12.
this
.country = country;
13.
this
.city = city;
14.
this
.street = street;
15.
this
.zipCode = zipcode;
16.
}
17.
/**
18.
* @hibernate.property
19.
* length = "12"
20.
*/
21.
public
String getCity() {
return
city;}
22.
public
void
setCity(String city) {
this
.city = city;}
23.
/**
24.
* @hibernate.property
25.
* length = "12"
26.
*/
27.
public
String getCountry() {
return
country;}
28.
public
void
setCountry(String country) {
this
.country = country;}
29.
/**
30.
* @hibernate.property
31.
* length = "6"
32.
*/
33.
public
String getZipCode() {
return
zipCode;}
34.
public
void
setZipCode(String number) {
this
.zipCode = number;}
35.
/**
36.
* @hibernate.property
37.
* length = "12"
38.
*/
39.
public
String getStreet() {
return
street;}
40.
public
void
setStreet(String street) {
this
.street = street;}
41.
public
String toString(){
42.
return
(
"
居住在
"
+ country + city+
"
市
"
+ street+
"
区
"
43.
+
"\n\t
邮政编码
: "
+ zipCode);
44.
}
45.
}
46.
xml
代码
1.
Person.hbm.xml
2.
3.
xml
version
=
"1.0"
encoding
=
"GBK"?>
4.
5.
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
6.
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
7.
<
hibernate-mapping
8.
>
9.
<
class
10.
name
=
"javamxj.hibernate.component.Person"
11.
dynamic-update
=
"false"
12.
dynamic-insert
=
"false"