第一部分:我们主要讨论hbm.xml文件的生成。
现在的生成工具很多,有hibernte tools,xdoclet,middlegen,hibernatesync...
个人比较偏好xdoclet 和 hibernatesync :)
一:xdoclet
http://xdoclet.sourceforge.net/
二:hibernatesync 是Eclipse的一个plugin
------------------------------------------------------------------------------------
先来贴一个Component
那我们就直接看代码吧.
[code]
package cn.ya.model;
import java.io.Serializable;
/**
* @author <a href="Martin'>mailto:[email protected]">Martin Xu</a> Date: 2005-6-2
* @hibernate.class
* table="tb_user"
*/
public class TUser2 implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Contact contact;
private Name name;
/**
* @hibernate.component
*/
public Contact getContact() {
return contact;
}
/**
* @param contact
* The contact to set.
*/
public void setContact(Contact contact) {
this.contact = contact;
}
/**
* @hibernate.id
* generator-class="native"
*/
public Integer getId() {
return id;
}
/**
* @param id
* The id to set.
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.component
*/
public Name getName() {
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(Name name) {
this.name = name;
}
}
[/code]
我们将用户资料分为 姓名 Name 和联系方式 Contact.
(hibernate.component 只有两个属性prefix 和 class ) class 属性xdoclet会自动去取的.所以很简单,我们只需要@hibernate.component就可以了.
[code]
/**
*
*/
package cn.ya.model;
import java.io.Serializable;
/**
* @author <a href="Martin'>mailto:[email protected]">Martin Xu</a> Date: 2005-6-2
*
*/
public class Name implements Serializable {
private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
/**
* @hibernate.property
*/
public final String getFirstname() {
return firstname;
}
/**
* @param firstname
* The firstname to set.
*/
public final void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @hibernate.property
*/
public final String getLastname() {
return lastname;
}
/**
* @param lastname
* The lastname to set.
*/
public final void setLastname(String lastname) {
this.lastname = lastname;
}
}
[/code]
[code]
package cn.ya.model;
import java.io.Serializable;
/**
* @author <a href="Martin'>mailto:[email protected]">Martin Xu</a> Date: 2005-6-2
*
*/
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;
private String address;
private String tel;
private String zipcode;
/**
* @hibernate.property
*/
public String getAddress() {
return address;
}
/**
* @param address The address to set.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @hibernate.property
*/
public String getTel() {
return tel;
}
/**
* @param tel The tel to set.
*/
public void setTel(String tel) {
this.tel = tel;
}
/**
* @hibernate.property
*/
public String getZipcode() {
return zipcode;
}
/**
* @param zipcode The zipcode to set.
*/
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
[/code]
我们只需在Contact 和 Name 写上hibernate.property就可以了,并不需要其他任何东西,xdoclet会自动把属性添加到component里面.
下面是最终产生的xml
[code]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="cn.ya.model.TUser2"
table="t_user2"
>
<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-TUser2.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<component
name="contact"
class="cn.ya.model.Contact"
>
<property
name="address"
type="java.lang.String"
update="true"
insert="true"
column="address"
/>
<property
name="tel"
type="java.lang.String"
update="true"
insert="true"
column="tel"
/>
<property
name="zipcode"
type="java.lang.String"
update="true"
insert="true"
column="zipcode"
/>
</component>
<component
name="name"
class="cn.ya.model.Name"
>
<property
name="firstname"
type="java.lang.String"
update="true"
insert="true"
column="firstname"
/>
<property
name="lastname"
type="java.lang.String"
update="true"
insert="true"
column="lastname"
/>
</component>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-TUser2.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
[/code]