| At line 1 changed 1 line. | 
| __Part I:__ [在AppFuse建立DAO和对象|CreateDAO_zh] - 一个建立对象(代表数据库的表)和把这些对象存储到数据库的Java类的教程。 | 
| __Part I:__ [在AppFuse建立DAO和POJO |CreateDAO_zh] - 一个建立对象(代表数据库的表)和把这些对象存储到数据库的Java类的教程。 | 
| At line 43 changed 1 line. | 
| %%note __注意:__ 直接拷贝本教程的代码 [在FireFox下无效|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes],但我们可以通过CTRL+Click选定一个代码所在的工作区(OS X下是Command+Click),然后再拷贝。%% | 
| %%note __注意:__ [不能在FireFox中|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes]直接拷贝本教程的代码 ,但我们可以通过CTRL+Click选定一个代码所在的工作区(OS X下是Command+Click),然后再拷贝。%% | 
| At line 62 changed 1 line. | 
| 这个类必须扩展[BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html],而这个BaseObject有三个抽象方法(equals(), hashCode()和toString())需要你在Person类里实现,前两个是Hibernate的需要。为了完成这部分工作最简单的方式是使用[Commonclipse|http://commonclipse.sf.net],关于这个工具更多的信息可以在[Lee Grey的网站|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]里看到,另外一个你可以使用的Eclipse的插件是[Commons4E|http://commons4e.berlios.de/],我还没有使用过,这里不便对其功能作出评论。 | 
| 这个类必须扩展[BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html],而这个BaseObject有三个抽象方法(equals(), hashCode()和toString())需要你在Person类里实现,前两个是Hibernate的需要。为了完成这部分工作最简单的方式是使用[Commonclipse|http://commonclipse.sf.net],关于这个工具更多的信息可以在[Lee Grey的站点|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]里看到,另外一个你可以使用的Eclipse的插件是[Commons4E|http://commons4e.berlios.de/],我还没有使用过,这里不便对其功能作出评论。 | 
| At line 78 changed 1 line. | 
| We also have to add a primary key mapping or XDoclet will puke when generating the mapping file.  Note that all @hibernate.* tags should be placed in the __getters'__ Javadocs of your POJOs. | 
| 我们也要增加主键的映射,否则XDoclet会在产生映射文件时出错,注意所有的@hibernate.*标签必须在__getters'__的Javadocs里面。 | 
| At line 93 changed 1 line. | 
| ;:%%(color: blue)''我使用{{generator-class="increment"}}而不使用{{generate-class="native"}} 是因为我对数据库使用"native"时[发现了一些问题|AppFuseOnDB2],如果你只是希望使用MySQL,__推荐使用"native"值__,本教程使用increment。''%% | 
| ;:%%(color: blue)''我使用{{generator-class="increment"}}而不使用{{generate-class="native"}} 是因为我对数据库使用"native"时[发现一些问题|AppFuseOnDB2],如果你只是希望使用MySQL,__推荐使用"native"值__,本教程使用increment。''%% | 
|  | 
|  | 
| !!使用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] | 
|  | 
| %%note <a name="appgen"></a>__注意:__从Appfuse版本1.6.1+开始包括了一个[AppGen]工具,可以用来生成本教程余下的所有的类的代码,不过,我们最好还是先过一遍教程再使用这个工具产生代码。%% | 
|  | 
| 现在,我们要创建一个DaoTest来测试我们的DAO的工作,等会儿,你说,我们还不曾创建DAO呢!,你说得对。无论如何,我发现[测试驱动开发|http://www.artima.com/intv/testdriven.html]大大的促进了软件质量,在许多年里我一直认为__在写代码之前写测试__是胡说八道,这看起来很愚蠢,但当我尝试之后我认为这样非常好,现在我按照测试驱动的方式工作完全因为我发现这样可以大大提高我软件开发的效率。 | 
|  | 
| 开始,我们在{{test/dao/**/dao}}目录下建立类{{PersonDaoTest.java}},这个类必须扩展[BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html],而BaseDAOTestCase这个类是JUnit类[TestCase|http://junit.sourceforge.net/javadoc/junit/framework/TestCase.html]的子类,这个类用来加载[Spring|http://www.springframework.org]的ApplicationContext(因为Spring把各个层绑定)和单元测试类同一目录下同你的测试类文件同名的.properties文件(ResourceBundle),这个属性文件的属性可以通过rb属性来访问。 | 
|  | 
| ;:%%(color: blue)''我经常拷贝(打开→另存为)一个已存在的测试(如UserDaoTest.java),然后查找/替换 [[Uu]ser为[[Pp]erson,或者任何其它需要替换的内容。''%% | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| package org.appfuse.dao; | 
|  | 
| import org.appfuse.model.Person; | 
| import org.springframework.dao.DataAccessException; | 
|  | 
| public class PersonDaoTest extends BaseDaoTestCase { | 
|  | 
| private Person person = null; | 
| private PersonDao dao = null; | 
|  | 
| public void setPersonDao(PersonDao dao) { | 
| this.dao = dao; | 
| } | 
| } | 
| }] | 
|  | 
| 以上是我们使用JUnit测试而初始化和销毁PersonDao的基本代码,对象ctx引用了Spring的ApplicationContext,它在[BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html]类的静态代码区里被初始化。 | 
|  | 
| 现在我们需要实际测试DAO中的CRUD(create, retrieve, update, delete)方法,为此我们需要为每个方法建立以test(全部小写)开头的测试方法,只要这个方法是公共的,返回类型是void,它们就会被我们build.xml中的Ant的<junit>任务调用,如下是一些简单的CRUD测试,需要注意的一点是所有的方法(或者叫做测试)必须是自治的,添加如下代码到文件{{PersonDaoTest.java}}: | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| public void testGetPerson() throws Exception { | 
| person = new Person(); | 
| person.setFirstName("Matt"); | 
| person.setLastName("Raible"); | 
|  | 
| dao.savePerson(person); | 
| assertNotNull(person.getId()); | 
|  | 
| person = dao.getPerson(person.getId()); | 
| assertEquals(person.getFirstName(), "Matt"); | 
| } | 
|  | 
| public void testSavePerson() throws Exception { | 
| person = dao.getPerson(new Long(1)); | 
| person.setFirstName("Matt"); | 
|  | 
| person.setLastName("Last Name Updated"); | 
|  | 
| dao.savePerson(person); | 
|  | 
| if (log.isDebugEnabled()) { | 
| log.debug("updated Person: " + person); | 
| } | 
|  | 
| assertEquals(person.getLastName(), "Last Name Updated"); | 
| } | 
|  | 
| public void testAddAndRemovePerson() throws Exception { | 
| person = new Person(); | 
| person.setFirstName("Bill"); | 
| person.setLastName("Joy"); | 
|  | 
| dao.savePerson(person); | 
|  | 
| assertEquals(person.getFirstName(), "Bill"); | 
| assertNotNull(person.getId()); | 
|  | 
| if (log.isDebugEnabled()) { | 
| log.debug("removing person..."); | 
| } | 
|  | 
| dao.removePerson(person.getId()); | 
|  | 
| try { | 
| person = dao.getPerson(person.getId()); | 
| fail("Person found in database"); | 
| } catch (DataAccessException dae) { | 
| log.debug("Expected exception: " + dae.getMessage()); | 
| assertNotNull(dae); | 
| } | 
| } | 
| }] | 
|  | 
|  | 
| ;:%%(color: blue)''在testGetPerson方法,我们创建了一个person并且调用get方法,我通常会增加一条我所需要的记录到数据库,因为在测试运行之前[DBUnit|http://www.dbunit.org]会为数据库准备测试数据,我们可以简单的在metadata/sql/sample-data.xml里添加测试所必须的记录''%% | 
|  | 
| <div style="color: blue !important; margin-left: 50px"> | 
| {{{ | 
| <table name='person'> | 
| <column>id</column> | 
| <column>first_name</column> | 
| <column>last_name</column> | 
| <row> | 
| <value>1</value> | 
| <value>Matt</value> | 
| <value>Raible</value> | 
| </row> | 
| </table> | 
| }}} | 
| </div> | 
|  | 
| ;:%%(color: blue)''通过这种方式你可以在testGetPerson方法里消除创建新纪录的动作,如果你愿意直接插入记录到数据库(使用SQL或者GUI),你可以用__ant db-export__和__cp {{db-export.xml metadata/sql/sample-data.xml}}__重新构建你的{{sample-data.xml}}文件。''%% | 
|  | 
| 在上面的例子里,你可以看到我们调用person.set*(value)来准备我们需要保存的对象,在这个例子里很简单,但是当你要插入10条必添字段(not-null="true")时就比较麻烦了,这就是我为什么要在BaseDaoTestCase使用ResourceBundle文件,只要在{{PersonDaoTest.java}}同一个目录创建一个{{PersonDaoTest.properties}}并且在里面定义你的属性值: | 
| ;:%%(color: blue)''我通常只是在Java里硬编码,但是这个.properties对于大对象很有用。''%% | 
| {{{ | 
| firstName=Matt | 
| lastName=Raible | 
| }}} | 
| 此时,你要通过调用BaseDaoTestCase.populate(java.lang.Object)方法来准备对象,而不是使用person.set*。 | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| person = new Person(); | 
| person = (Person) populate(person); | 
| }] | 
|  | 
| 在目前情况下,还不可以编译PersonDaoTest,因为在类路径里还没有PersonDao.class,我们需要创建它。PersonDao.java是一个接口,PersonDaoHibernate.java是它的Hibernate实现,让我们继续,开始创建。 | 
|  | 
| !!创建一个对对象执行CRUD操作的新DAO[#4] | 
| 马上,在{{src/dao/**/dao}}目录里建立PersonDao.java接口,并且指定所有实现类要实现的基本CRUD操作,为了显示方便,我已经去掉了所有JavaDocs。 | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| package org.appfuse.dao; | 
|  | 
| import org.appfuse.model.Person; | 
|  | 
| public interface PersonDao extends Dao { | 
| public Person getPerson(Long personId); | 
| public void savePerson(Person person); | 
| public void removePerson(Long personId); | 
| } | 
| }] | 
|  | 
| 注意,在以上的方法声明上并没有exceptions说明,这是因为[Spring|http://www.springframework.org]使用RuntimeExceptions来包裹Exceptions的方式,此时,你已经可以使用__ant compile-dao__来编译{{src/dao}}和{{test/dao}}下的所有源文件,然而当你运行__ant test-dao -Dtestcase=PersonDao__进行测试时,你会得到一个错误:<span style="color: red">No bean named 'personDao' is defined</span>,这是一个Spring的错误,说明你必须在{{applicationContext-hibernate.xml}}指定一个名字为''personDAO''的bean,在此之前我们需要创建PersonDao的实现类。 | 
|  | 
| ;:''运行dao测试的ant任务叫做__test-dao__,如果你传递testcase参数(用__-Dtestcase=name__),它会查看{{**/*${testcase}*}}允许我们传递Person、PersonDao、或者PersonDaoTest以及所有会执行PersonDaoTest的类。'' | 
|  | 
| 让我们创建一个实现PersonDao的类PersonDaoHibernate并使用Hibernate来get/save/delete这个Person对象,为此,我们在{{src/dao/**/dao/hibernate}}创建一个新类{{PersonDaoHibernate.java}},它应该扩展[BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html],并且实现PersonDao。''为了简洁,省略Javadocs。'' | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| package org.appfuse.dao.hibernate; | 
|  | 
| import org.appfuse.model.Person; | 
| import org.appfuse.dao.PersonDao; | 
| import org.springframework.orm.ObjectRetrievalFailureException; | 
|  | 
| public class PersonDaoHibernate extends BaseDaoHibernate implements PersonDao { | 
|  | 
| public Person getPerson(Long id) { | 
| Person person = (Person) getHibernateTemplate().get(Person.class, id); | 
|  | 
| if (person == null) { | 
| throw new ObjectRetrievalFailureException(Person.class, id); | 
| } | 
|  | 
| return person; | 
| } | 
|  | 
| public void savePerson(Person person) { | 
| getHibernateTemplate().saveOrUpdate(person); | 
| } | 
|  | 
| public void removePerson(Long id) { | 
| // object must be loaded before it can be deleted | 
| getHibernateTemplate().delete(getPerson(id)); | 
| } | 
| } | 
| }] | 
|  | 
| 现在,如果你运行__ant test-dao -Dtestcase=PersonDao__,你会得到同样的错误,我们必须配置Spring来让它知道PersonDaoHibernate是PersonDao的实现,同样的,我们也要告诉它还有个Person对象。 | 
|  | 
| !!配置Spring中的Person和PersonDao [#5] | 
|  | 
| 首先我们要告诉Spring所有Hibernate文件的位置,为此,打开{{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}},在以下代码块添加{{"Person.hbm.xml"}}。 | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| <property name="mappingResources"> | 
| <list> | 
| <value>org/appfuse/model/Person.hbm.xml</value> | 
| <value>org/appfuse/model/Role.hbm.xml</value> | 
| <value>org/appfuse/model/User.hbm.xml</value> | 
| </list> | 
| </property> | 
| }] | 
|  | 
| 现在我们需要添加一些XML数据来绑定PersonDaoHibernate到PersonDao,为此,添加如下代码到文件底部: | 
|  | 
| [{Java2HtmlPlugin | 
|  | 
| <!-- PersonDao: Hibernate implementation --> | 
| <bean id="personDao" class="org.appfuse.dao.hibernate.PersonDaoHibernate"> | 
| <property name="sessionFactory"><ref local="sessionFactory"/></property> | 
| </bean> | 
| }] | 
|  | 
| ;:''你也可以为<bean>使用__autowire="byName"__属性来消除"sessionFactory"属性''。%%(color: blue)''从个人来讲,我喜欢在XML文件里保留对象的依赖。''%% | 
|  | 
| !!运行DaoTest[#6] | 
| 保存所有修改的文件,运行__ant test-dao -Dtestcase=PersonDao__。 | 
|  | 
| __Yeah Baby, Yeah:__ | 
| %%(color:green)BUILD SUCCESSFUL\\ | 
| Total time: 9 seconds%% | 
|  | 
| ---- | 
|  | 
| ''下一部分:'' __Part II:__ [创建新的Manager|CreateManager_zh] - 是一个建立类似于[Session Facades|http://java.sun.com/blueprints/patterns/SessionFacade.html]的,但不使用EJBs的业务Facade说明,这个facades用来建立从前端到DAO层的联系。 |