read AppFuse 11-实践-业务信息person的建立
● 按照AppFuse帮助,进行AppFuse开发实践
(1) 创建POJO
package org.dudu.model;
/**
* @hibernate.class table="person"
*/
public class Person extends BaseObject
{
private Long id;
private String firstName;
private String lastName;
/**
* @return Returns the id
* @hibernate.id column="id"
* generator-class="increment" unsaved-value="null"
*/
public long getId() {
return id;
}
/**
* @return Returns thd first name
* @struts.validator type="required"
* @hibernate.property column="first_name" length="50" not-null="true"
*/
public String getFirstName() {
return firstName;
}
/**
* @return Returns the last name
* @struts.validator type="required"
* @hibernate.property column="last_name" length="50" not-null="true"
*/
public String getLastName() {
return lastName;
}
/**
* @param l
*/
public void setId(long l) {
id = l;
}
/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}
/**
* @param string
*/
public void setLastName(String string) {
lastName = string;
}
/* (非 Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
// TODO 自动生成方法存根
return false;
}
/* (非 Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return 0;
}
public String toString() {
return null;
}
}
Ø Hibernate是一个对象/关系(O/R) 框架,它允许你将一个Java对象同一个表关联。
Ø 要确保对象的id属性的getter是当前类的第一个方法,否则生成的JSP的页面可能会有一个脚本错误
Ø 注意:所有的 @hibernate.* tag必须放在你的POJO的getter的Javadoc中
Ø 如果你想更改列的长度, 更改@hibernate.property标记的length属性。如果你希望它是一个必填的字段(NOT NULL),那么你要添加not-null="true"。
Ø 添加column属性的唯一原因是:表的列名和对象的属性名字不一致。如果他们一致,就不需要添加column属性。[引用]
(2) 使用Ant根据Person对象创建一个person新表和名为Person.hbm.xml的映射文件。
E:\dudu>ant setup-db
Buildfile: build.xml
init:
db-create:
[echo] Detected MySQL, creating database...
[echo] Creating database with: mysql-create.sql
[echo] URL: jdbc:mysql://localhost/mysql
[sql] Executing file: E:\dudu\metadata\sql\mysql-create.sql
[sql] 3 of 3 SQL statements executed successfully
clean:
[echo] Cleaning build and distribution directories
[delete] Deleting directory E:\dudu\build
[delete] Deleting directory E:\dudu\dist
[delete] Deleting: E:\dudu\database.properties
build.properties.missing:
prepare:
[echo] Preparing target directory 'E:\dudu/build/dudu'
[mkdir] Created dir: E:\dudu\build\dudu
[mkdir] Created dir: E:\dudu\build\dudu\WEB-INF
[mkdir] Created dir: E:\dudu\dist\webapps
hibernatedoclet:
[hibernatedoclet] (XDocletMain.start 47 ) Running <hibernate/
>
[hibernatedoclet] Generating mapping file for org.dudu.model.User.
[hibernatedoclet] org.dudu.model.User
[hibernatedoclet] Generating mapping file for org.dudu.model.Person.
[hibernatedoclet] org.dudu.model.Person
[hibernatedoclet] Generating mapping file for org.dudu.model.Role.
[hibernatedoclet] org.dudu.model.Role
[hibernatedoclet] Generating mapping file for org.dudu.model.UserCookie.
[hibernatedoclet] org.dudu.model.UserCookie
compile-dao:
compile-module:
[echo] Compiling dao...
[mkdir] Created dir: E:\dudu\build\dao\classes
[mkdir] Created dir: E:\dudu\build\test\dao\classes
[javac] Compiling 16 source files to E:\dudu\build\dao\classes
[javac] Note: E:\dudu\src\dao\org\dudu\model\User.java uses unchecked or uns
afe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
package-dao:
[copy] Copying 1 file to E:\dudu\build\dao\gen
[jar] Building jar: E:\dudu\dist\dudu-dao.jar
db-prepare:
generate.database.properties:
[echo] generating database.properties from build.properties
[propertyfile] Creating new property file: E:\dudu\database.properties
[schemaexport] (cfg.Environment 478 ) Hibernate 2.1.7
[schemaexport] (cfg.Environment 507 ) hibernate.properties n
ot found
[schemaexport] (cfg.Environment 538 ) using CGLIB reflection
optimizer
[schemaexport] (cfg.Environment 567 ) using JDK 1.4 java.sql
.Timestamp handling
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\Person.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud
u.model.Person -> person
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\Role.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud
u.model.Role -> role
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\User.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud
u.model.User -> app_user
[schemaexport] (cfg.Binder 572 ) Mapping collection: or
g.dudu.model.User.roles -> user_role
[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\UserCookie.hbm.xml
[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud
u.model.UserCookie -> user_cookie
[schemaexport] (dialect.Dialect 86 ) Using dialect: net.sf.
hibernate.dialect.MySQLDialect
[schemaexport] (cfg.Configuration 632 ) processing one-to-many
association mappings
[schemaexport] (cfg.Configuration 641 ) processing one-to-one
association property references
[schemaexport] (cfg.Configuration 666 ) processing foreign key
constraints
[schemaexport] (cfg.Configuration 632 ) processing one-to-many
association mappings
[schemaexport] (cfg.Configuration 641 ) processing one-to-one
association property references
[schemaexport] (cfg.Configuration 666 ) processing foreign key
constraints
[schemaexport] (hbm2ddl.SchemaExport 98 ) Running hbm2ddl schema
export
[schemaexport] (hbm2ddl.SchemaExport 112 ) writing generated sche
ma to file: create-tables.sql
[schemaexport] (hbm2ddl.SchemaExport 117 ) exporting generated sc
hema to database
[schemaexport] (connection.DriverManagerConnectionProvider 42 ) Using Hibernate
built-in connection pool (not for production use!)
[schemaexport] (connection.DriverManagerConnectionProvider 43 ) Hibernate conne
ction pool size: 20
[schemaexport] (connection.DriverManagerConnectionProvider 77 ) using driver: c
om.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/dudu?autoReconnect=true&useU
nicode=true&characterEncoding=utf-8
[schemaexport] (connection.DriverManagerConnectionProvider 78 ) connection prop
erties: {user=test, password=test, show_sql=true}
[schemaexport] alter table user_role drop foreign key FK143BF46A14048CB4;
[schemaexport] alter table user_role drop foreign key FK143BF46AF02988D6;
[schemaexport] drop table if exists user_role;
[schemaexport] drop table if exists app_user;
[schemaexport] drop table if exists person;
[schemaexport] drop table if exists user_cookie;
[schemaexport] drop table if exists role;
[schemaexport] create table user_role (
[schemaexport] username varchar(20) not null,
[schemaexport] role_name varchar(20) not null,
[schemaexport] primary key (username, role_name)
[schemaexport] );
[schemaexport] create table app_user (
[schemaexport] username varchar(20) not null,
[schemaexport] version integer not null,
[schemaexport] password varchar(255) not null,
[schemaexport] first_name varchar(50) not null,
[schemaexport] last_name varchar(50) not null,
[schemaexport] address varchar(150),
[schemaexport] city varchar(50) not null,
[schemaexport] province varchar(100),
[schemaexport] country varchar(100),
[schemaexport] postal_code varchar(15) not null,
[schemaexport] email varchar(255) not null unique,
[schemaexport] phone_number varchar(255),
[schemaexport] website varchar(255),
[schemaexport] password_hint varchar(255),
[schemaexport] primary key (username)
[schemaexport] );
[schemaexport] create table person (
[schemaexport] id bigint not null,
[schemaexport] first_name varchar(50) not null,
[schemaexport] last_name varchar(50) not null,
[schemaexport] primary key (id)
[schemaexport] );
[schemaexport] create table user_cookie (
[schemaexport] id bigint not null,
[schemaexport] username varchar(30) not null,
[schemaexport] cookie_id varchar(100) not null,
[schemaexport] date_created datetime not null,
[schemaexport] primary key (id)
[schemaexport] );
[schemaexport] create table role (
[schemaexport] name varchar(20) not null,
[schemaexport] version integer not null,
[schemaexport] description varchar(255),
[schemaexport] primary key (name)
[schemaexport] );
[schemaexport] alter table user_role add index FK143BF46A14048CB4 (role_name), a
dd constraint FK143BF46A14048CB4 foreign key (role_name) references role (name);
[schemaexport] alter table user_role add index FK143BF46AF02988D6 (username), ad
d constraint FK143BF46AF02988D6 foreign key (username) references app_user (user
name);
[schemaexport] create index user_cookie_username_cookie_id on user_cookie (usern
ame, cookie_id);
[schemaexport] (hbm2ddl.SchemaExport 160 ) schema export complete
[schemaexport] (connection.DriverManagerConnectionProvider 143 ) cleaning up con
nection pool: jdbc:mysql://localhost/dudu?autoReconnect=true&useUnicode=true&cha
racterEncoding=utf-8
db-load:
(connection.DriverManagerConnectionProvider 143 ) cleaning up connection pool: j
dbc:mysql://localhost/dudu?autoReconnect=true&useUnicode=true&characterEncoding=
utf-8
[dbunit] Executing operation: CLEAN_INSERT
[dbunit] on file: E:\dudu\metadata\sql\sample-data.xml
[dbunit] with format: xml
setup-db:
BUILD SUCCESSFUL
Total time: 1 minute 6 seconds
Ø 生成的person表
mysql> desc person;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | bigint(20) | | PRI | 0 | |
| first_name | varchar(50) | | | | |
| last_name | varchar(50) | | | | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Ø 生成的Person..hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="org.dudu.model.Person"
table="person"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Person.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="first_name"
length="50"
not-null="true"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="last_name"
length="50"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Person.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>