Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7594365 建立时间:2006年5月29日 |

| |
[Java Open Source]Integrate Compass, Appfuse, DisplayTag Tutorial Part 3 软件技术
lhwork 发表于 2007/1/23 9:01:02 |
In this tutorial, we use jdk 5.0 annotation to help us makes Compass Resource Mapping easier
All codes in part 3 could be download here
I remove lib, extra, docs folder to reduce size.
Step 1. Create Models with Compass annotation
1. Create a model named "Document.java" and add codes below
------ start here ------ package org.myapp.model;
import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.ToStringBuilder;import org.apache.commons.lang.builder.CompareToBuilder;import org.compass.annotations.Searchable;import org.compass.annotations.SearchableId;import org.compass.annotations.SearchableMetaData;import org.compass.annotations.SearchableProperty;import org.compass.annotations.SearchableReference;
/** * @hibernate.class table="document" */@Searchable(alias = "document")public class Document extends BaseObject implements Comparable<Document>, Serializable{
private static final long serialVersionUID = 1L; private Long id; private String title; private String keyWords; private Integer version; private Date publishDate; private List chapters = new ArrayList(); /** * @return Returns the chapters. * * @hibernate.bag name="entries" lazy="false" cascade="all" * @hibernate.collection-key column="document_id" * @hibernate.collection-one-to-many class="org.myapp.model.Chapter" */ @SearchableReference(refAlias = "chapter") public List getChapters() { return chapters; } /** * @hibernate.id column="id" generator-class="native" unsaved-value="null" */ @SearchableId public Long getId() { return id; } /** * @hibernate.property */ @SearchableProperty(name = "keyWords") public String getKeyWords() { return keyWords; } /** * @hibernate.property */ public Date getPublishDate() { return publishDate; } /** * @hibernate.property */ @SearchableProperty(name = "title") public String getTitle() { return title; } /** * @hibernate.property */ @SearchableProperty(name = "version") public Integer getVersion() { return version; } public void setChapters(List chapters) { this.chapters = chapters; } public void setId(Long id) { this.id = id; } public void setKeyWords(String keyWords) { this.keyWords = keyWords; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } public void setTitle(String title) { this.title = title; } public void setVersion(Integer version) { this.version = version; } @Override public boolean equals(final Object other) { if (!(other instanceof Document)) return false; Document castOther = (Document) other; return new EqualsBuilder().append(id, castOther.id).append(title, castOther.title).append(keyWords, castOther.keyWords).append( version, castOther.version).append(publishDate, castOther.publishDate).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder().append(id).append(title).append(keyWords) .append(version).append(publishDate).toHashCode(); } @Override public String toString() { return new ToStringBuilder(this).append("id", id) .append("title", title).append("keyWords", keyWords).append( "version", version).append("publishDate", publishDate) .toString(); } public int compareTo(final Document other) { return new CompareToBuilder().append(id, other.id).append(title, other.title).append(keyWords, other.keyWords).append(version, other.version).append(publishDate, other.publishDate) .toComparison(); } }
------ end here------
2. Create a model named "Chapter.java" and add codes below
------ start here ------ package org.myapp.model;
import java.io.Serializable;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.ToStringBuilder;import org.apache.commons.lang.builder.CompareToBuilder;import org.compass.annotations.Searchable;import org.compass.annotations.SearchableId;import org.compass.annotations.SearchableProperty;
/** * @hibernate.class table="chapter" */@Searchable(alias = "chapter")public class Chapter extends BaseObject implements Comparable<Chapter>, Serializable{
private static final long serialVersionUID = 1L; private Long id; private String content; private String title; /** * @hibernate.property */ @SearchableProperty(name = "content") public String getContent() { return content; } /** * @hibernate.id column="id" generator-class="native" unsaved-value="null" */ @SearchableId public Long getId() { return id; } /** * @hibernate.property */ @SearchableProperty(name = "title") public String getTitle() { return title; } public void setContent(String content) { this.content = content; } public void setId(Long id) { this.id = id; } public void setTitle(String title) { this.title = title; } @Override public boolean equals(final Object other) { if (!(other instanceof Chapter)) return false; Chapter castOther = (Chapter) other; return new EqualsBuilder().append(id, castOther.id).append(content, castOther.content).append(title, castOther.title).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder().append(id).append(content).append(title) .toHashCode(); } @Override public String toString() { return new ToStringBuilder(this).append("id", id).append("content", content).append("title", title).toString(); } public int compareTo(final Chapter other) { return new CompareToBuilder().append(id, other.id).append(content, other.content).append(title, other.title).toComparison(); } }
------ end here------
4. Run "ant db-prepare" to create tables
I create two models named "Document" and "Chapter" whitch has one-to-many mapping.
Also notice that we use Compass annotation tag (@Searchable, @SearchableId, @SearchableProperty ...) to declare fields.
Remeber to change your java compiler compliance to 5.0, nor you will get complained from you IDE. 5. Edit applicationContext-hibernate.xml.
Look for "sessionFactory" bean configuration and add below. <value>org/myapp/model/Document.hbm.xml</value> <value>org/myapp/model/Chapter.hbm.xml</value>
4. Edit sample-data.xml and add below
------ start here ------
<table name='document'> <column>id</column> <column>title</column> <column>keyWords</column> <column>version</column> <column>publishDate</column> <row> <value>1</value> <value>title01</value> <value>keyWords01</value> <value>1</value> <value>2006-09-06</value> </row> </table> <table name='chapter'> <column>id</column> <column>title</column> <column>content</column> <column>document_id</column> <row> <value>1</value> <value>title001</value> <value>content001</value> <value>1</value> </row> <row> <value>2</value> <value>title002</value> <value>content002</value> <value>1</value> </row> </table>
------ end here------
This step erases Document and Chapter in db.
Step 2. Configure spring
1. Edit applicationContext-service.xml
add <bean id="annotationConfiguration" class="org.compass.annotations.config.CompassAnnotationsConfiguration"></bean> and look for <bean id="compass" class="org.compass.spring.LocalCompassBean"> add two new properties under "bean" tag <property name="classMappings"> <list> <value>org.myapp.model.Document</value> <value>org.myapp.model.Chapter</value> </list> </property> <property name="compassConfiguration" ref="annotationConfiguration"/> Note:
The configuration is from a project named SpringSide, in section 2.11.1 and 2.11.2 http://www.springside.org.cn http://www.springside.org.cn/docs/reference/Compass1.htm http://www.springside.org.cn/docs/reference/Compass3.htm 2. Run "ant delete-lucene-index" and "ant create-lucene-index"
Check your index file location, you should have three new folders named "user", "document" and "chapter" They contain index. In my case, they are in "C:\compass\myapp\index\chapter", "C:\compass\myapp\index\document", "C:\compass\myapp\index\user" Summary
In this tutorial, we use annotation to simplify cpm file generation. In next part, we will create struts actions for create, update and view. |
|
|