Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
This is version 98.
It is not the current version, and thus it cannot be edited. About this tutorialThis is a tutorial to show how to create and manage Hibernate relationships within AppFuse. This tutorial was written using AppFuse 1.8. All of the code for this tutorial is downloadable. To begin, I created a new application with an app/db name of blog/blog.
Table of Contents
Create Weblog.java, Entry.java and add XDoclet tags [#1]The Weblog object is used to indentify a person's blog. This class has the following properties:
The Entry object is used to contain a listing of a person's blog entries in their Weblog. This class contains the following properties:
Below is a class diagram of these two objects, as well as the others you'll create in this tutorial.
The first thing you need to do in this tutorial is these two object to persist. Create both a Weblog.java class and an Entry.java class (in the src/dao/**/model directory). The necessary XDoclet tags for these entities is included on the getter method's javadoc. You can download these files using the links below. Note that javadocs have been eliminated for brevity.
[Many-to-One] Create a new Category object and modify Entry.java to use it [#2]A category object will act as an entity for persisting category information about weblog entries. Each category can have many entries. This class contains the following properties:
The many-to-one relationship between Entry and Category can be established using XDoclet tags. Hibernate relationships can be established on either side of the relationship or bi-directionally. For this tutorial, this relationship is maintained by a categoryId property and a Category object in Entry. The list of possible categories for a weblog entry will eventually be represented as a drop-down on the UI. Add the following code to your Entry.java class.
[One-to-Many] A Weblog object can have many Entries [#3]Now you'll modify the Weblog object and Entry object to represent the multiplicity of a weblog that can have many entries. This relationship is set on the Weblog class using a java.util.List. XDoclet tags are used to establish this relationship using a bag as the Hibernate collection type. Add the following code to your Weblog.java class.
Modify the Entry class is so it contains a foreign key to it's parent Weblog class.
[Many-to-Many] [#4]The Weblog system in this tutorial allows one additional bit of functionality. A particular Weblog can have many Users. Basically the idea is of a shared weblog that is a place where many users can express themselves about a particular topic of interest. For this bit of functionality the User object will be modified to have a many-to-many relationship with Weblog. Add the following users property and accessor methods to Weblog.java.
User modificationsIn addition, to allow navigation from a Weblog object to its list of users, you need to add a Set of Weblogs to the User object - allowing that path of navigation as well. Modify User.java to add a weblogs Set and accessor methods. You might notice the cascade="none" attribute. This is because this system doesn't currenly allow a weblog to be edited from the user screen and vise-versa. You'll change this in the section where you create the UI.
[DAO Layer] [#5]In order to test that all of this works, you need to create some test classes, add some sample data, develop some DAO interfaces, and create Hibernate DAO classes. Test ClassesThere are a number of ways to construct the DAO layer for this application. You could have a WeblogDAO that handles CRUD for all entities, or you could have separate DAOs for each entity. This tutorial uses the separate DAO approach because this seems to be the most common method used by AppFuse users. Personally, I tend to group my logical features into a single DAO with many methods. Download the following three test classes and put them in your test/dao/**/dao directory. All of these tests rely on some sample data, so add the following XML to metadata/sql/sample-data.xml. <table name='weblog'> <column>weblog_id</column> <column>blog_title</column> <column>date_created</column> <column>username</column> <row> <value>1</value> <value><![CDATA[Sponge Bob is Cool]]></value> <value>2004-03-31</value> <value>tomcat</value> </row> <row> <value>2</value> <value><![CDATA[Java Development = Fun]]></value> <value>2005-01-05</value> <value>mraible</value> </row> </table> <table name='weblog_user'> <column>weblog_id</column> <column>username</column> <row> <value>1</value> <value>tomcat</value> </row> <row> <value>1</value> <value>mraible</value> </row> <row> <value>2</value> <value>mraible</value> </row> </table> <table name='category'> <column>category_id</column> <column>name</column> <column>description</column> <row> <value>1</value> <value><![CDATA[Struts v. Spring MVC]]></value> <value><![CDATA[Comparing implementations of the MVC Design Pattern]]></value> </row> <row> <value>2</value> <value><![CDATA[Cycling Notes]]></value> <value><![CDATA[All about cycling in the US.]]></value> </row> <row> <value>3</value> <value><![CDATA[Cyclocross]]></value> <value><![CDATA[Bog Trotters Unite!]]></value> </row> </table> <table name='entry'> <column>entry_id</column> <column>entry_text</column> <column>time_created</column> <column>weblog_id</column> <column>category_id</column> <row> <value>1</value> <value><![CDATA[Testing]]></value> <value>2005-04-11</value> <value>1</value> <value>1</value> </row> <row> <value>2</value> <value><![CDATA[Test Value]]></value> <value>2005-04-12</value> <value>1</value> <value>1</value> </row> <row> <value>3</value> <value><![CDATA[Test Value 3]]></value> <value>2005-04-12</value> <value>2</value> <value>3</value> </row> </table> DAO InterfacesBefore any of these tests will compile, you need to create the DAO interfaces and their implementations. WeblogDAO InterfaceCreate a WeblogDAO.java interface in src/dao/**/dao:
EntryDAO Interface
CategoryDAO Interface
DAO ImplementationThen create the Hibernate implementations of the interfaces in the src/dao/**/dao/hibernate directory. WeblogDAOHibernate
EntryDAOHibernate
CategoryDAOHibernate
Configure SpringModifications need to be made in src/org/appfuse/dao/hibernate/applicationContext-hibernate.xml for the 3 new entities you created in this tutorial.
Also, you need to add the "weblogDAO", "entryDAO", and "categoryDAO" bean definitions.
After making these changes, you should be able to run ant setup-db test-dao. All of the tests should pass successfully. Yeah baby, Yeah!
BUILD SUCCESSFUL
Total time: 1 minute 6 seconds Next Up: Part II: Create Weblog UI - Creating a UI to manage the relationships created in this tutorial. Attachments:
|