接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/10/28/3168079.aspx
先看看compass的技术资料:http://www.compass-project.org/docs/2.0.2/reference/html/
老实说,从我第一次接触她,就有一种熟悉的感觉,因为他跟Hibernate的API真的是太像了。
下面的代码来自compass自带的例子,我稍微改造了一下。
代码如下:
Author.java
- package com.tutorial;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- public class Author implements Identifiable {
- private Long id;
- private Name name;
- private Date birthdate;
- private String[] keywords;
- private List<Book> books = new ArrayList<Book>();
- public Author() {
- }
- public Date getBirthdate() {
- return birthdate;
- }
- public void setBirthdate(Date birthdate) {
- this.birthdate = birthdate;
- }
- public List<Book> getBooks() {
- return books;
- }
- public void setBooks(List<Book> books) {
- this.books = books;
- }
- public void addBook(Book book) {
- books.add(book);
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public Name getName() {
- return name;
- }
- public void setName(Name name) {
- this.name = name;
- }
- public String[] getKeywords() {
- return keywords;
- }
- public void setKeywords(String[] keywords) {
- this.keywords = keywords;
- }
- public String toString() {
- return name.toString();
- }
- }
Book.java
- package com.tutorial;
- import java.util.Date;
- public class Book implements Identifiable {
- private Long id;
- private String title;
- private Date publishDate;
- private String[] keywords;
- private String summary;
- public Book() {
- }
- public Book(String title, Date publishDate, String summary, String[] keywords) {
- this.title = title;
- this.publishDate = publishDate;
- this.summary = summary;
- this.keywords = keywords;
- }
- public String[] getKeywords() {
- return keywords;
- }
- public void setKeywords(String[] keywords) {
- this.keywords = keywords;
- }
- public Date getPublishDate() {
- return publishDate;
- }
- public void setPublishDate(Date publishDate) {
- this.publishDate = publishDate;
- }
- public String getSummary() {
- return summary;
- }
- public void setSummary(String summary) {
- this.summary = summary;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String toString() {
- return title;
- }
- }
Identifiable.java
- package com.tutorial;
- public interface Identifiable {
- Long getId();
- }
Name.java
- package com.tutorial;
- public class Name {
- private String title;
- private String firstName;
- private String lastName;
- public Name() {
- }
- public Name(String title, String firstName, String lastName) {
- this.title = title;
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public boolean equals(Object other) {
-
- if (this == other) {
- return true;
- }
- if (!(other instanceof Name)) {
- return false;
- }
- Name otherName = (Name) other;
- if (title != null) {
- if (!title.equals(otherName.getTitle())) {
- return false;
- }
- }
- if (firstName != null) {
- if (!firstName.equals(otherName.getFirstName())) {
- return false;
- }
- }
- if (lastName != null) {
- if (!lastName.equals(otherName.getLastName())) {
- return false;
- }
- }
- return true;
- }
- public int hashCode() {
- int hash = 1;
- hash = hash * 31 + title == null ? 0 : title.hashCode();
- hash = hash * 31 + firstName == null ? 0 : firstName.hashCode();
- hash = hash * 31 + lastName == null ? 0 : lastName.hashCode();
- return hash;
- }
- public String toString() {
- return title + " " + " " + firstName + " " + lastName;
- }
- }
Library.java
Author.cpm.xml
- <?xml version="1.0"?>
- <!DOCTYPE compass-core-mapping PUBLIC
- "-//Compass/Compass Core Mapping DTD 2.0//EN"
- "http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd">
- <compass-core-mapping package="com.tutorial">
- <class name="Author" alias="${library.author}">
- <id name="id" />
- <constant>
- <meta-data>${library.type}</meta-data>
- <meta-data-value>${library.type.mdPerson}</meta-data-value>
- <meta-data-value>${library.type.mdAuthor}</meta-data-value>
- </constant>
- <property name="keywords">
- <meta-data boost="2">${library.keyword}</meta-data>
- </property>
- <component name="name" ref-alias="${library.name.md}" />
- <property name="birthdate">
- <meta-data>${library.birthdate}</meta-data>
- </property>
- <reference name="books" ref-alias="${library.book}" />
- </class>
- <class name="Name" alias="${library.name}" root="false">
- <property name="title">
- <meta-data>${library.titleName}</meta-data>
- </property>
- <property name="firstName">
- <meta-data>${library.firstName}</meta-data>
- <meta-data>${library.name}</meta-data>
- </property>
- <property name="lastName">
- <meta-data>${library.lastName}</meta-data>
- <meta-data>${library.name}</meta-data>
- </property>
- </class>
- </compass-core-mapping>
Book.cpm.xml
- <?xml version="1.0"?>
- <!DOCTYPE compass-core-mapping PUBLIC
- "-//Compass/Compass Core Mapping DTD 2.0//EN"
- "http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd">
- <compass-core-mapping package="com.tutorial">
- <class name="Book" alias="${library.book}">
-
- <id name="id" />
- <property name="keywords">
- <meta-data boost="2">${library.keyword}</meta-data>
- </property>
-
- <property name="title">
- <meta-data>${library.title}</meta-data>
- </property>
- <property name="publishDate">
- <meta-data>${library.publishDate}</meta-data>
- </property>
- <property name="summary">
- <meta-data>${library.summary}</meta-data>
- </property>
-
- </class>
-
- </compass-core-mapping>
library.cmd.xml
compass.cfg.xml
- <!DOCTYPE compass-core-configuration PUBLIC
- "-//Compass/Compass Core Configuration DTD 2.0//EN"
- "http://www.compass-project.org/dtd/compass-core-configuration-2.0.dtd">
- <compass-core-configuration>
- <compass>
-
- <setting name="compass.engine.connection">target/index</setting>
-
- <meta-data resource="com/tutorial/library.cmd.xml" />
-
- </compass>
- </compass-core-configuration>
测试代码:
LibraryTests.java
- package com.demo;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import org.compass.core.Compass;
- import org.compass.core.CompassCallbackWithoutResult;
- import org.compass.core.CompassException;
- import org.compass.core.CompassHits;
- import org.compass.core.CompassQueryBuilder;
- import org.compass.core.CompassSession;
- import org.compass.core.CompassTemplate;
- import org.compass.core.CompassTransaction;
- import org.compass.core.Resource;
- import org.compass.core.config.CompassConfiguration;
- import com.tutorial.Author;
- import com.tutorial.Book;
- import com.tutorial.Identifiable;
- import com.tutorial.Library;
- import com.tutorial.Name;
- public class LibraryTests {
- public static void main(String[] args){
-
- CompassConfiguration config = new CompassConfiguration();
- config.configure("/com/tutorial/compass.cfg.xml");
- config.addClass(Author.class).addClass(Book.class);
- Compass compass = config.buildCompass();
- compass.getSearchEngineIndexManager().deleteIndex();
- compass.getSearchEngineIndexManager().createIndex();
-
- try {
- setUpData(compass);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- CompassSession session = compass.openSession();
- CompassTransaction tx = null;
- try {
- tx = session.beginTransaction();
- Author author = (Author) session.load(Author.class, 1);
-
- SimpleDateFormat sdf = new SimpleDateFormat(Library.MetaData.Birthdate.Format);
- System.out.println(sdf.format(author.getBirthdate()));
- System.out.println(author.getName().getFirstName());
- System.out.println(author.getBooks().size());
-
-
-
-
- tx.commit();
- } catch (Exception e) {
- if (tx != null) {
- tx.rollback();
- }
- } finally {
- session.close();
- }
-
- CompassTemplate compassTemplate = new CompassTemplate(compass);
- compassTemplate.execute(new CompassCallbackWithoutResult() {
- protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
- String query = "fiercely";
- CompassHits hits = session.find(query);
- System.out.println("Found [" + hits.getLength() + "] hits for [" + query + "] query");
- System.out.println("======================================================");
- for (int i = 0; i < hits.getLength(); i++) {
- Object value = hits.data(i);
- Resource resource = hits.resource(i);
- System.out.println("ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]");
- System.out.println(":::: " + value);
- System.out.println("");
- }
- hits.close();
- }
- });
-
- compassTemplate = new CompassTemplate(compass);
- compassTemplate.execute(new CompassCallbackWithoutResult() {
- protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
- CompassQueryBuilder queryBuilder = session.queryBuilder();
- CompassHits hits = queryBuilder.bool().addMust(queryBuilder.term("book.id", 1))
-
- .toQuery().hits();
-
- System.out.println("Found [" + hits.getLength() + "] hits for [" + "Jack" + "] query");
- System.out.println("======================================================");
- for (int i = 0; i < hits.getLength(); i++) {
- Object value = hits.data(i);
- Resource resource = hits.resource(i);
- System.out.println("ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]");
- System.out.println(":::: " + value);
- System.out.println("");
- }
- hits.close();
- }
- });
-
- }
-
- public static void setUpData(Compass compass) throws Exception {
-
- CompassSession session = compass.openSession();
- CompassTransaction tx = session.beginTransaction();
-
- Author jackLondon = new Author();
- jackLondon.setId(new Long(1));
- jackLondon.setName(new Name("Mr", "Jack", "London"));
- Calendar c = Calendar.getInstance();
- c.set(1876, 0, 12);
- jackLondon.setBirthdate(c.getTime());
- jackLondon.setKeywords(new String[] { "american author" });
- Book whiteFang = new Book();
- whiteFang.setId(new Long(1));
- whiteFang.setTitle("White Fang");
- c.set(1906, 0, 1);
- whiteFang.setPublishDate(c.getTime());
- whiteFang.setSummary("The remarkable story of a fiercely independent creature of the wild");
- whiteFang.setKeywords(new String[] { "jack london", "call of the wild" });
- jackLondon.addBook(whiteFang);
- session.save(whiteFang);
- Book callOfTheWild = new Book();
- callOfTheWild.setId(new Long(2));
- callOfTheWild.setTitle("The Call of the Wild");
- c.set(1903, 0, 1);
- callOfTheWild.setPublishDate(c.getTime());
- callOfTheWild.setSummary("The Call of the Wild is a tale about unbreakable spirit");
- callOfTheWild.setKeywords(new String[] { "jack london", "buck", "white fang" });
- jackLondon.addBook(callOfTheWild);
- session.save(callOfTheWild);
- session.save(jackLondon);
-
- Author jamesClavell = new Author();
- jamesClavell.setId(new Long(2));
- jamesClavell.setName(new Name("Mr", "James", "Clavell"));
- c.set(1924, 9, 10);
- jamesClavell.setBirthdate(c.getTime());
- jamesClavell.setKeywords(new String[] { "far east", "shogun", "japan", "hong kong" });
- Book shogun = new Book();
- shogun.setId(new Long(3));
- shogun.setTitle("Shogun");
- c.set(1975, 0, 1);
- shogun.setPublishDate(c.getTime());
- shogun.setSummary("A story of a hero who is not a person but a place and a time,"
- + " medieval Japan on the threshold of becoming a sea power");
- shogun.setKeywords(new String[] { "james clavell", "Blackthorne", "Toranaga", "japan" });
- jamesClavell.addBook(shogun);
- session.save(shogun);
- Book taipan = new Book();
- taipan.setId(new Long(4));
- taipan.setTitle("Taipan");
- c.set(1966, 0, 1);
- taipan.setPublishDate(c.getTime());
- taipan.setSummary("Tai-Pan is chinese for \"supreme leader\". This is the man with real power "
- + "to his hands. And such a Tai-Pan is Dirk Struan who is obsessed by his plan to make Hong Kong "
- + "the \"jewel in the crown of her British Majesty\". In 1841 he achieves his goal but he has many "
- + "enemies who try to destroy his plans. Will they succeed?");
- taipan.setKeywords(new String[] { "james clavell", "Dirk Struan", "joss", "hong kong" });
- jamesClavell.addBook(taipan);
- session.save(taipan);
- session.save(jamesClavell);
-
- tx.commit();
- session.close();
- }
- }
发表于 @ 2008年10月29日 16:27:00|评论(loading...)|收藏