| At line 94 added 84 lines. |
|
|
| !!使用Ant根据对象产生数据库表[#2] |
| 在这种情况下,你可以通过运行__ant setup-db__来建立person表,这个任务会产生文件{{Person.hbm.xml}}并且会建立叫做"person"的表,从Ant的控制台窗口,你可以看到Hibernate为你建立的表结构的内容。 |
| {{{ |
| [schemaexport] create table person ( |
| [schemaexport] id bigint not null, |
| [schemaexport] primary key (id) |
| [schemaexport] ); |
| }}} |
|
| 如果你查看Hibernate生成的文件{{Person.hbm.xml}},可以到{{build/dao/gen/**/model}}目录,这里是{{Person.hbm.xml}}的内容(目前的内容): |
|
| [{Java2HtmlPlugin |
|
| <?xml version="1.0"?> |
|
| <!DOCTYPE hibernate-mapping PUBLIC |
| "-//Hibernate/Hibernate Mapping DTD 2.0//EN" |
| "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> |
|
| <hibernate-mapping> |
| <class |
| name="org.appfuse.model.Person" |
| table="person" |
| dynamic-update="false" |
| dynamic-insert="false" |
| > |
|
| <id |
| name="id" |
| column="id" |
| type="java.lang.Long" |
| unsaved-value="null" |
| > |
| <generator class="increment"> |
| </generator> |
| </id> |
|
| <!-- |
| 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> |
| }] |
|
| 现在我们要为其它的字段(first_name, last_name)添加额外的[@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)]标签: |
|
|
| [{Java2HtmlPlugin |
|
| /** |
| * @hibernate.property column="first_name" length="50" |
| */ |
| public String getFirstName() { |
| return this.firstName; |
| } |
|
| /** |
| * @hibernate.property column="last_name" length="50" |
| */ |
| public String getLastName() { |
| return this.lastName; |
| } |
| }] |
|
| 在这个例子里,添加''column''属性的唯一原因是因为这个字段名与它的属性名不相同,如果他们相同,你没有必要来指定''column''属性,关于其它可以使用的标签请看[@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)]。 |
|
| 再次运行__ant setup-db__把新加的属性加到数据库表里。 |
|
| {{{[schemaexport] create table person ( |
| [schemaexport] id bigint not null, |
| [schemaexport] first_name varchar(50), |
| [schemaexport] last_name varchar(50), |
| [schemaexport] primary key (id) |
| [schemaexport] );}}} |
|
| 如果期望修改字段的长度,修改@hibernate.property标签的''length''属性,如果希望把字段改为必添字段(NOT NULL),可以增加属性not-null="true"。 |
|
| !!建立新的DaoTest来对你的DAO运行JUnit测试[#3] |