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 2.
It is not the current version, and thus it cannot be edited. About this TutorialThis 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:
AppTracker 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 AppTracker's architecture. Table of Contents
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]
|