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


This is version 3. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


About this Tutorial

This tutorial will show you how to create a new table in the database, and how to create Java code to access this table. We will create an object and then some more classes to persist (save/retrieve/delete) that object from the database. In Java speak, we call the object a Plain Old Java Object (a.k.a. a POJO. This object basically represents a database table. The other classes will be:
  • A Data Access Object (a.k.a. a DAO), an Interface and a Hibernate Implementation
  • A JUnit class to test our DAO is working

AppFuse uses Hibernate for it's persistence layer. Hibernate is an Object/Relational (O/R) Framework that allows you to relate your Java Objects to database tables. It allows you to very easily perform CRUD (Create, Retrieve, Update, Delete) on your objects. Let's get started on creating a new Object, DAO and Test in AppFuse's architecture.

Table of Contents

  • [1] Create a new Object and add XDoclet tags
  • [2] Create a new database table from the object using Ant
  • [3] Create a new DaoTest to run JUnit tests on your DAO
  • [3] Create a new DAO to perform CRUD on the object
  • [4] Notify Hibernate that this object exists in ServiceLocator (for tests) and in hibernate.cfg.xml (for webapp)
  • [5] Run the DaoTest

Create a new Object and add XDoclet tags [#1]

The first thing we need to do is create an object to persist. Let's create a simple "Person" object that has an id, a firstName and a lastName (as properties).
package org.appfuse.persistence;

public class Person extends BaseObject {
	private Long id;
	private String firstName;
	private String lastName;
	
	/* 
	Generate your getters and setters using
	your favorite IDE:
	    Eclipse: Right-click -> Source - > 
	             Generate Getters and Setters   
	    IDEA:    ?? I can't seem to figure it out right now
}

In the code snippet above, we're extending BaseObject because it has the following useful methods: toString(), equals(), hashCode() - the latter two are required by Hibernate.

Now that we have this POJO created, we need to add XDoclet tags to generate the Hibernate mapping file. This mapping file is used by Hibernate to map objects -> tables and properties (variables) -> columns.

Create a new database table from the object using Ant [#2]

Create a new DaoTest to run JUnit tests on your DAO [#3]

Create a new DAO to perform CRUD on the object [#4]

Notify Hibernate that this object exists [#5]

Run the DaoTest [#6]



Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:30 MST by MattRaible.