Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_pt
CreateDAO_es
CreateDAO_pt
CreateDAO_sp
CreateDAOiBATIS
CreateManager
CreateManager_es
CreateManager_ko
CreateManager_zh
...and 3 more




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO


Difference between version 123 and version 122:

At line 11 changed 2 lines.
%%note __NOTE:__ If you're using MySQL and you want to use transactions (which you probably will) you have to use InnoDB tables. To do this, add the following to your mysql configuration file (<span style="color: green">{{/etc/my.cnf}}</span> or <span style="color: green">{{c:\Windows\my.ini}}</span>). The 2nd setting (for a UTF-8 character set) is needed for 4.1.7+.
<span style="color: green">{{{
%%note __NOTE:__ If you're using MySQL and you want to use transactions (which you probably will) you have to use InnoDB tables. To do this, add the following to your mysql configuration file ({{/etc/my.cnf}} or {{c:\Windows\my.ini}}). The 2nd setting (for a UTF-8 character set) is needed for 4.1.7+.
{{{
At line 16 changed 2 lines.
}}</span>}
If you're using PostgreSQL and you get confusing errors about batch processing, try turning it off by adding <span style="color: green">{{&lt;prop key="hibernate.jdbc.batch_size">0&lt;/prop>}}</span> to your <span style="color: green">{{src/dao/**/hibernate/applicationContext-hibernate.xml}}</span> file.
}}}
If you're using PostgreSQL and you get confusing errors about batch processing, try turning it off by adding {{&lt;prop key="hibernate.jdbc.batch_size">0&lt;/prop>}} to your {{src/dao/**/hibernate/applicationContext-hibernate.xml}} file.
At line 22 changed 1 line.
;:''You can also use [iBATIS|http://ibatis.com] as a persistence framework option. To install iBATIS in AppFuse, view the README.txt in <span style="color: green">{{extras/ibatis}}</span>. If you're using iBATIS over Hibernate, I expect you have your reasons and are familiar with the framework. I also expect that you can figure out how to adapt this tutorial to work with iBATIS. ;-)''
;:''You can also use [iBATIS|http://ibatis.com] as a persistence framework option. To install iBATIS in AppFuse, view the README.txt in {{extras/ibatis}}. If you're using iBATIS over Hibernate, I expect you have your reasons and are familiar with the framework. I also expect that you can figure out how to adapt this tutorial to work with iBATIS. ;-)''
At line 27 changed 1 line.
;:References to files, directories and packages which exist in your source tree: <span style="color: green">{{build.xml}}</span>.
;:References to files, directories and packages which exist in your source tree: {{build.xml}}.
At line 44 changed 1 line.
The first thing we need to do is create an object to persist. Let's create a simple "Person" object (in the <span style="color: green">{{src/dao/**/model}}</span> directory) that has an id, a firstName and a lastName (as properties).
The first thing we need to do is create an object to persist. Let's create a simple "Person" object (in the {{src/dao/**/model}} directory) that has an id, a firstName and a lastName (as properties).
At line 94 changed 1 line.
;:%%(color: blue)''I'm using <span style="color: green">{{generator-class="increment"}}</span> instead of <span style="color: green">{{generate-class="native"}}</span> because I [found some issues|AppFuseOnDB2] when using "native" on other databases. If you only plan on using MySQL, I __recommend you use the "native" value__. This tutorial uses increment.''%%
;:%%(color: blue)''I'm using {{generator-class="increment"}} instead of {{generate-class="native"}} because I [found some issues|AppFuseOnDB2] when using "native" on other databases. If you only plan on using MySQL, I __recommend you use the "native" value__. This tutorial uses increment.''%%
At line 97 changed 2 lines.
At this point, you can create the person table by running __ant setup-db__. This task creates the <span style="color: green">{{Person.hbm.xml}}</span> file and creates a database table called "person". From the ant console, you can see the table schema the Hibernate creates for you:
<span style="color: green">{{{
At this point, you can create the person table by running __ant setup-db__. This task creates the {{Person.hbm.xml}} file and creates a database table called "person". From the ant console, you can see the table schema the Hibernate creates for you:
{{{
At line 103 changed 1 line.
}}</span>}
}}}
At line 105 changed 1 line.
If you want to look at the <span style="color: green">{{Person.hbm.xml}}</span> file that Hibernate generates for you, look in the <span style="color: green">{{build/dao/gen/**/model}}</span> directory. Here's the contents of <span style="color: green">{{Person.hbm.xml}}</span> (so far):
If you want to look at the {{Person.hbm.xml}} file that Hibernate generates for you, look in the {{build/dao/gen/**/model}} directory. Here's the contents of {{Person.hbm.xml}} (so far):
At line 168 changed 1 line.
<span style="color: green">{{{[schemaexport] create table person (
{{{[schemaexport] create table person (
At line 173 changed 1 line.
[schemaexport] );}}</span>}
[schemaexport] );}}}
At line 188 changed 2 lines.
# Create your POJO (in the model directory) and <a href="#ref-CreateDAO-5">configure the mapping file</a> in <span style="color: green">{{applicationContext-hibernate.xml}}</span>.
# cd into the ''<span style="color: green">{{extras/appgen}}</span>'' directory and run __ant -Dobject.name=Person -Dappgen.type=pojo__. In this example, the __Person__ class should exist in your <span style="color: green">{{model}}</span> package. This generates all the files you create in the tutorials on this site (for your chosen web framework).
# Create your POJO (in the model directory) and <a href="#ref-CreateDAO-5">configure the mapping file</a> in {{applicationContext-hibernate.xml}}.
# cd into the ''{{extras/appgen}}'' directory and run __ant -Dobject.name=Person -Dappgen.type=pojo__. In this example, the __Person__ class should exist in your {{model}} package. This generates all the files you create in the tutorials on this site (for your chosen web framework).
At line 192 changed 1 line.
The reason for the "lowercase" parameter is to rename the JSPs to begin with a lowercase letter. If I tried to rename them and change the filename programmatically, it took 1MB worth of BSF and Rhino JARs (+5 lines of code) and this just seemed easier. Speaking of JSPs - it's up to you to modify the <span style="color: green">{{*Form.jsp}}</span> and make it look pretty. This is covered in Step 5 of each respective web framework's "Create Action/Controller" tutorial: <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=CreateActions#ref-CreateAction-5">Struts</a>, <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=SpringControllers#ref-SpringControllers-5">Spring</a> and <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=WebWorkActions#ref-WebWorkActions-5">WebWork</a>.
The reason for the "lowercase" parameter is to rename the JSPs to begin with a lowercase letter. If I tried to rename them and change the filename programmatically, it took 1MB worth of BSF and Rhino JARs (+5 lines of code) and this just seemed easier. Speaking of JSPs - it's up to you to modify the {{*Form.jsp}} and make it look pretty. This is covered in Step 5 of each respective web framework's "Create Action/Controller" tutorial: <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=CreateActions#ref-CreateAction-5">Struts</a>, <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=SpringControllers#ref-SpringControllers-5">Spring</a> and <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=WebWorkActions#ref-WebWorkActions-5">WebWork</a>.
At line 194 changed 1 line.
__NOTE:__ If you'd like to generate all the DAOs/Managers/Tests, run __ant install-detailed__ instead of __ant install__. Before you install anything, the files will be created in the <span style="color: green">{{extras/appgen/build/gen}}</span> directory (in case you want to look at them before installing). If you just want to test the tool, you can cd to this directory and run __ant test__ to see the contents of these tutorials created.
__NOTE:__ If you'd like to generate all the DAOs/Managers/Tests, run __ant install-detailed__ instead of __ant install__. Before you install anything, the files will be created in the {{extras/appgen/build/gen}} directory (in case you want to look at them before installing). If you just want to test the tool, you can cd to this directory and run __ant test__ to see the contents of these tutorials created.
At line 200 changed 1 line.
To start, create a <span style="color: green">{{PersonDaoTest.java}}</span> class in the <span style="color: green">{{test/dao/**/dao}}</span> directory. This class should extend [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html], which already exists in this package. This parent class is used to load [Spring's|http://www.springframework.org] ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your <span style="color: green">{{*Test.class}}</span>. In this example, if you put a <span style="color: green">{{PersonDaoTest.properties}}</span> file in the same directory as <span style="color: green">{{PersonDaoTest.java}}</span>, this file's properties will be available via an "rb" variable.
To start, create a {{PersonDaoTest.java}} class in the {{test/dao/**/dao}} directory. This class should extend [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html], which already exists in this package. This parent class is used to load [Spring's|http://www.springframework.org] ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your {{*Test.class}}. In this example, if you put a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}}, this file's properties will be available via an "rb" variable.
At line 232 changed 1 line.
Now we need to actually test that the CRUD (create, retrieve, update, delete) methods work in our DAO. To do this we created methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our &lt;junit&gt; task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your <span style="color: green">{{PersonDaoTest.java}}</span> file:
Now we need to actually test that the CRUD (create, retrieve, update, delete) methods work in our DAO. To do this we created methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our &lt;junit&gt; task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your {{PersonDaoTest.java}} file:
At line 292 changed 1 line.
<span style="color: green">{{{
{{{
At line 303 changed 1 line.
}}</span>}
}}}
At line 306 changed 1 line.
;:%%(color: blue)''This way, you can eliminate the "create new" functionality in the testGetPerson method. If you'd rather add this record directly into the database (via SQL or a GUI), you can rebuild your <span style="color: green">{{sample-data.xml}}</span> file using __ant db-export__ and then __cp <span style="color: green">{{db-export.xml metadata/sql/sample-data.xml}}</span>__.''%%
;:%%(color: blue)''This way, you can eliminate the "create new" functionality in the testGetPerson method. If you'd rather add this record directly into the database (via SQL or a GUI), you can rebuild your {{sample-data.xml}} file using __ant db-export__ and then __cp {{db-export.xml metadata/sql/sample-data.xml}}__.''%%
At line 308 changed 1 line.
In the above example, you can see that we're calling person.set*(value) to populate our object before saving it. This is easy in this example, but it could get quite cumbersome if we're persisting an object with 10 required fields (not-null="true"). This is why I created the ResourceBundle in the BaseDaoTestCase. Simply create a <span style="color: green">{{PersonDaoTest.properties}}</span> file in the same directory as <span style="color: green">{{PersonDaoTest.java}}</span> and define your property values inside it:
In the above example, you can see that we're calling person.set*(value) to populate our object before saving it. This is easy in this example, but it could get quite cumbersome if we're persisting an object with 10 required fields (not-null="true"). This is why I created the ResourceBundle in the BaseDaoTestCase. Simply create a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}} and define your property values inside it:
At line 310 changed 1 line.
<span style="color: green">{{{
{{{
At line 313 changed 1 line.
}}</span>}
}}}
At line 325 changed 1 line.
First off, create a PersonDao.java interface in the <span style="color: green">{{src/dao/**/dao}}</span> directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.''
First off, create a PersonDao.java interface in the {{src/dao/**/dao}} directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.''
At line 340 changed 1 line.
Notice in the class above there are no exceptions on the method signatures. This is due to the power of [Spring|http://www.springframework.org] and how it wraps Exceptions with RuntimeExceptions. At this point, you should be able to compile all the source in <span style="color: green">{{src/dao}}</span> and <span style="color: green">{{test/dao}}</span> using __ant compile-dao__. However, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get an error: <span style="color: red">No bean named 'personDao' is defined</span>. This is an error message from Spring - indicating that we need to specify a bean named ''personDAO'' in <span style="color: green">{{applicationContext-hibernate.xml}}</span>. Before we do that, we need to create the PersonDao implementation class.
Notice in the class above there are no exceptions on the method signatures. This is due to the power of [Spring|http://www.springframework.org] and how it wraps Exceptions with RuntimeExceptions. At this point, you should be able to compile all the source in {{src/dao}} and {{test/dao}} using __ant compile-dao__. However, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get an error: <span style="color: red">No bean named 'personDao' is defined</span>. This is an error message from Spring - indicating that we need to specify a bean named ''personDAO'' in {{applicationContext-hibernate.xml}}. Before we do that, we need to create the PersonDao implementation class.
At line 342 changed 1 line.
;:''The ant task for running dao tests is called __test-dao__. If you pass in a testcase parameter (using __-Dtestcase=name__), it will look for <span style="color: green">{{**/*${testcase}*}}</span> - allowing us to pass in Person, PersonDao, or PersonDaoTest - all of which will execute the PersonDaoTest class.''
;:''The ant task for running dao tests is called __test-dao__. If you pass in a testcase parameter (using __-Dtestcase=name__), it will look for {{**/*${testcase}*}} - allowing us to pass in Person, PersonDao, or PersonDaoTest - all of which will execute the PersonDaoTest class.''
At line 344 changed 1 line.
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in <span style="color: green">{{src/dao/**/dao/hibernate}}</span> and name it <span style="color: green">{{PersonDAOHibernate.java}}</span>. It should extend [BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html] and implement PersonDAO. ''Javadocs eliminated for brevity.''
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in {{src/dao/**/dao/hibernate}} and name it {{PersonDAOHibernate.java}}. It should extend [BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html] and implement PersonDAO. ''Javadocs eliminated for brevity.''
At line 382 changed 1 line.
First, we need to tell Spring where the Hibernate mapping file is located. To do this, open <span style="color: green">{{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}}</span> and add "<span style="color: green">{{Person.hbm.xml}}</span>" to the following code block.
First, we need to tell Spring where the Hibernate mapping file is located. To do this, open {{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}} and add {{"Person.hbm.xml"}} to the following code block.

Back to CreateDAO, or to the Page History.