基于Appfuse的持久层开发

来源:互联网 发布:js 像素转换成毫米 编辑:程序博客网 时间:2024/05/17 08:09

Appfuse Persistence(基于Appfuse的持久层开发)

Skip to end of metadataGo to start of metadata

About this Tutorial

This tutorial creates a new database table and the Java code to access that table.

You create an object and then some more classes to persist (save/retrieve/delete) that object from the database. In Java speak, this object is called a Plain Old Java Object (POJO). This object basically represents a database table. With AppFuse, there is a Generics-based DAO and Manager that will CRUD all objects for you. The only time you'll need to create DAOs is when you want custom behavior or if you need finders.

AppFuse uses Hibernate for its default 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.

JPA  You can also use JPA as a persistence framework option. To use JPA implementation, see the Using JPA tutorial.

To get started creating a new Object and table in AppFuse's project structure, please complete the instructions below.

Table of Contents

  1. Create a new POJO and add JPA Annotations
  2. Create a new database table from the object using Maven

Create a new POJO and add JPA Annotations

The first thing you need to do is create an object to persist. Create a simple "Person" object (in the src/main/java/**/model directory for the basic archetypes or the core/src/main/java/**/model directory for the modular archetypes) that has an id, a firstName and a lastName (as properties). For recommend package-naming conventions, see the FAQ. These tutorials use "org.appfuse.tutorial" as the root package name.

packageorg.appfuse.tutorial.model;
 
importorg.appfuse.model.BaseObject;
 
importjavax.persistence.Entity;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.GeneratedValue;
importjavax.persistence.Column;
 
publicclass Person extendsBaseObject {
    privateLong id;
    privateString firstName;
    privateString lastName;
 
    /*
     Generate your getters and setters using your favorite IDE:
     In Eclipse:
     Right-click -> Source -> Generate Getters and Setters
     Generate toString(), hashCode() and equals() methods using your favorite IDE:
     In Eclipse:
     Right-click -> Source -> Generate toString(), hashCode(), equals()
   */
}

Extending BaseObject is optional, but recommended as a good practice to force creation of toString()equals() and hashCode() methods. If you plan to put this object into the user's session, or expose it through a web service, you should implement java.io.Serializable as well.

When generating or writing equals() and hashCode() methods, you don't want to include your object's primary key. See Hibernate's Equals and Hascode for more information.


Now that you have this POJO created, you need to add JPA annotations. These annotations are used by Hibernate to map objects → tables and properties (variables) → columns.

If you're new to Hibernate and Annotations, you might want to read An Introduction to Hibernate 3 Annotations by John Ferguson Smart.

First of all, add an @Entity annotation that signifies what table this object relates to. The "name" is optional; the class name is used if it's not specified. Make sure you import annotations from javax.persistence.* rather than from Hibernate.

@Entity
publicclass Person extendsBaseObject {

If you specify a name value for your @Entity annotation (for example @Entity(name="person")), this will be the alias for HQL queries. If you don't specify this value, the name will match the short name of your class (Person). If you want to change the table name that's generated, use the@Table annotation with a "name" value.

You also have to add an @Id annotation to signify the primary key. The @GeneratedValue annotation should also be specified to indicate the primary key generation strategy.

@Id@GeneratedValue(strategy = GenerationType.AUTO)
publicLong getId() {
    returnthis.id;
}

For the rest of the fields, you aren't required to annotate them unless you want to 1) exclude them from being persisted (with @Transient) or 2) want to change their column name or other attributes. To change the column names, use the @Column annotation. Add the @Column annotation to both thegetFirstName() and getLastName() methods.

@Column(name="first_name", length=50)
publicString getFirstName() {
    returnthis.firstName;
}
...
@Column(name="last_name", length=50)
publicString getLastName() {
    returnthis.lastName;
}
Annotations on fields

You can also put JPA annotations on fields instead of getters. However, you should be aware that if you add field-level annotations, method-level annotations will be ignored.

Create a new database table from the object using Maven

Open src/main/resources/hibernate.cfg.xml for the basic archetypes (or core/src/main/resources/hibernate.cfg.xml for the modular archetypes) and register your Person object with the following XML:

<mappingclass="org.appfuse.tutorial.model.Person"/>

Save the file and run mvn test-compile hibernate3:hbm2ddl from the command line. For modular projects, make sure you run this command from the "core" directory. This will generate your database schema with the "person" table included.


以上教程摘自Appfuse官网:http://appfuse.org/display/APF/Persistence