用Xdoclet为Component生成HBM文件需注意几个地方,以下代码中用红色标出:
Java代码:
Person.java
/**
* @hibernate.class
* table="t_user"
*
* @author wxh
*
*/
public class Person implements Serializable {
private Integer id;
private String firstName;
private String lastName;
private PhoneNumber phone;
public Person() {
}
/**
* @hibernate.property
*
* @return
*/
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @hibernate.id
* generator-class="native"
*
* @return
*/
public Integer getId() {
return id;
}
private void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property
*
* @return
*/
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @hibernate.component
*
* @return
*/
public PhoneNumber getPhone() {
return phone;
}
public void setPhone(PhoneNumber phone) {
this.phone = phone;
}
}
PhoneNumber.java
public class PhoneNumber implements java.io.Serializable {
private String countryCode;
private String areaCode;
private String number;
public PhoneNumber() {
}
/**
* @hibernate.property
* column="areacode"
* length="3"
*
* @return
*/
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
/**
* @hibernate.property
* column="countrycode"
* length="3"
* update="false"
* insert="false"
*
* @return
*/
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
/**
* @hibernate.property
* colomn="number"
* length="15"
*
* @return
*/
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public boolean equals(Object o) {
if (this==o) {
return true;
}
if (!(o instanceof PhoneNumber)) {
return false;
}
PhoneNumber ref = (PhoneNumber)o;
return new EqualsBuilder()
.appendSuper(super.equals(o))
.append(this.countryCode,ref.countryCode)
.append(this.areaCode,ref.areaCode)
.append(this.number,ref.number)
.isEquals();
}
public int hasCode() {
return new HashCodeBuilder(17,31)
.appendSuper(super.hashCode())
.append(this.countryCode)
.append(this.areaCode)
.append(this.number)
.toHashCode();
}
}
生成Hbm文件如下:
<hibernate-mapping
>
<class
name="com.wxh.types.Person"
table="t_user"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Integer"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Person.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="firstName"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="lastName"
/>
<component
name="phone"
class="com.wxh.types.PhoneNumber"
>
<property
name="areaCode"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="areacode"
length="3"
/>
<property
name="countryCode"
type="java.lang.String"
update="false"
insert="false"
access="property"
column="countrycode"
length="3"
/>
<property
name="number"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="number"
length="15"
/>
</component>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Person.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>