基于Appfuse的持久层开发
来源:互联网 发布:js 像素转换成毫米 编辑:程序博客网 时间:2024/05/17 08:09
Appfuse Persistence(基于Appfuse的持久层开发)
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.
To get started creating a new Object and table in AppFuse's project structure, please complete the instructions below.
Table of Contents
- Create a new POJO and add JPA Annotations
- 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.
package
org.appfuse.tutorial.model;
import
org.appfuse.model.BaseObject;
import
javax.persistence.Entity;
import
javax.persistence.GenerationType;
import
javax.persistence.Id;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Column;
public
class
Person
extends
BaseObject {
private
Long id;
private
String firstName;
private
String 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()
*/
}
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.
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
public
class
Person
extends
BaseObject {
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)
public
Long getId() {
return
this
.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
)
public
String getFirstName() {
return
this
.firstName;
}
...
@Column
(name=
"last_name"
, length=
50
)
public
String getLastName() {
return
this
.lastName;
}
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:
<
mapping
class
=
"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
- 基于Appfuse的持久层开发
- 基于Hibernate的持久层的组成
- 基于iBatis的通用持久层对象
- 基于iBatis的通用持久层对象
- 基于Hibernate的数据持久层应用简述
- 基于持久层的通用性组装树型结构
- Mybatis 基于Java的持久层框架(一)
- Mybatis 基于Java的持久层框架(二)
- Hibernate基于持久层框架的DAO模式应用
- MyEclipse开发Hibernate持久层
- [构建自己的持久层]六、开始开发DAO之一
- EJB3.0中 持久化层的开发
- EJB3.0中 持久化层的开发
- 基于 appfuse 的学生信息管理系统
- FreeMarker基于view层的开发
- [构建自己的持久层]----公开我的持久层
- 关于持久层的迷惑!
- Coyahogo的持久层访问
- 大屏幕更爽 iPhone4/iPad2视频输出实测
- Xmanager 连接 solaris 10
- 路由器相关知识
- poj 2112 Optimal Milking
- 【ORACLE】 11g 数据库表导出再导入10g 时空表不能导出的问题
- 基于Appfuse的持久层开发
- 如何经营水果超市
- 开源 免费 java CMS - FreeCMS1.3-标签 infoPage
- C# 大宝宝IP地址便捷修改器 v1.1
- 浅谈C/C++堆栈指引——C/C++堆栈很强大
- 第18章STL动态数组类
- android String 资源 你所不知道的
- python中eval, exec, execfile,和compile
- structs实现的MVC结构