重拾Compass之Compass2.0.2版本自带例子分析

接上例: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

  1. package com.tutorial;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. public class Author implements Identifiable {
  6.     private Long id;
  7.     private Name name;
  8.     private Date birthdate;
  9.     private String[] keywords;
  10.     private List<Book> books = new ArrayList<Book>();
  11.     public Author() {
  12.     }
  13.     public Date getBirthdate() {
  14.         return birthdate;
  15.     }
  16.     public void setBirthdate(Date birthdate) {
  17.         this.birthdate = birthdate;
  18.     }
  19.     public List<Book> getBooks() {
  20.         return books;
  21.     }
  22.     public void setBooks(List<Book> books) {
  23.         this.books = books;
  24.     }
  25.     public void addBook(Book book) {
  26.         books.add(book);
  27.     }
  28.     public Long getId() {
  29.         return id;
  30.     }
  31.     public void setId(Long id) {
  32.         this.id = id;
  33.     }
  34.     public Name getName() {
  35.         return name;
  36.     }
  37.     public void setName(Name name) {
  38.         this.name = name;
  39.     }
  40.     public String[] getKeywords() {
  41.         return keywords;
  42.     }
  43.     public void setKeywords(String[] keywords) {
  44.         this.keywords = keywords;
  45.     }
  46.     public String toString() {
  47.         return name.toString();
  48.     }
  49. }

Book.java

  1. package com.tutorial;
  2. import java.util.Date;
  3. public class Book implements Identifiable {
  4.     private Long id;
  5.     private String title;
  6.     private Date publishDate;
  7.     private String[] keywords;
  8.     private String summary;
  9.     public Book() {
  10.     }
  11.     public Book(String title, Date publishDate, String summary, String[] keywords) {
  12.         this.title = title;
  13.         this.publishDate = publishDate;
  14.         this.summary = summary;
  15.         this.keywords = keywords;
  16.     }
  17.     public String[] getKeywords() {
  18.         return keywords;
  19.     }
  20.     public void setKeywords(String[] keywords) {
  21.         this.keywords = keywords;
  22.     }
  23.     public Date getPublishDate() {
  24.         return publishDate;
  25.     }
  26.     public void setPublishDate(Date publishDate) {
  27.         this.publishDate = publishDate;
  28.     }
  29.     public String getSummary() {
  30.         return summary;
  31.     }
  32.     public void setSummary(String summary) {
  33.         this.summary = summary;
  34.     }
  35.     public String getTitle() {
  36.         return title;
  37.     }
  38.     public void setTitle(String title) {
  39.         this.title = title;
  40.     }
  41.     public Long getId() {
  42.         return id;
  43.     }
  44.     public void setId(Long id) {
  45.         this.id = id;
  46.     }
  47.     public String toString() {
  48.         return title;
  49.     }
  50. }

Identifiable.java

  1. package com.tutorial;
  2. public interface Identifiable {
  3.     Long getId();
  4. }

Name.java

  1. package com.tutorial;
  2. public class Name {
  3.     private String title;
  4.     private String firstName;
  5.     private String lastName;
  6.     public Name() {
  7.     }
  8.     public Name(String title, String firstName, String lastName) {
  9.         this.title = title;
  10.         this.firstName = firstName;
  11.         this.lastName = lastName;
  12.     }
  13.     public String getFirstName() {
  14.         return firstName;
  15.     }
  16.     public void setFirstName(String firstName) {
  17.         this.firstName = firstName;
  18.     }
  19.     public String getLastName() {
  20.         return lastName;
  21.     }
  22.     public void setLastName(String lastName) {
  23.         this.lastName = lastName;
  24.     }
  25.     public String getTitle() {
  26.         return title;
  27.     }
  28.     public void setTitle(String title) {
  29.         this.title = title;
  30.     }
  31.     public boolean equals(Object other) {
  32.         
  33.         if (this == other) {
  34.             return true;
  35.         }
  36.         if (!(other instanceof Name)) {
  37.             return false;
  38.         }
  39.         Name otherName = (Name) other;
  40.         if (title != null) {
  41.             if (!title.equals(otherName.getTitle())) {
  42.                 return false;
  43.             }
  44.         }
  45.         if (firstName != null) {
  46.             if (!firstName.equals(otherName.getFirstName())) {
  47.                 return false;
  48.             }
  49.         }
  50.         if (lastName != null) {
  51.             if (!lastName.equals(otherName.getLastName())) {
  52.                 return false;
  53.             }
  54.         }
  55.         return true;
  56.     }
  57.     public int hashCode() {
  58.         int hash = 1;
  59.         hash = hash * 31 + title == null ? 0 : title.hashCode();
  60.         hash = hash * 31 + firstName == null ? 0 : firstName.hashCode();
  61.         hash = hash * 31 + lastName == null ? 0 : lastName.hashCode();
  62.         return hash;
  63.     }
  64.     public String toString() {
  65.         return title + " " + " " + firstName + " " + lastName;
  66.     }
  67. }

Library.java

  1. package com.tutorial;
  2. public final class Library {
  3.     public static final class Group {
  4.         public static final String Id = "library";
  5.         public static final String DispayName = "Library Meta Data";
  6.         public static final String Uri = "http://compass/toturial";
  7.     }
  8.     public static final class Alias {
  9.         public static final class Book {
  10.             public static final String Id = "book";
  11.             public static final String Name = "book";
  12.             public static final String DisplayName = "Book";
  13.             public static final String Uri = "http://compass/toturial/alias/book";
  14.             public static final String GroupId = "library";
  15.         }
  16.         public static final class Name {
  17.             public static final String Id = "name";
  18.             public static final String Name = "name";
  19.             public static final String DisplayName = "Name";
  20.             public static final String Uri = "http://compass/toturial/alias/name";
  21.             public static final String GroupId = "library";
  22.         }
  23.         public static final class Author {
  24.             public static final String Id = "author";
  25.             public static final String Name = "author";
  26.             public static final String DisplayName = "Author";
  27.             public static final String Uri = "http://compass/toturial/alias/author";
  28.             public static final String GroupId = "library";
  29.         }
  30.     }
  31.     public static final class MetaData {
  32.         public static final class Summary {
  33.             public static final String Id = "summary";
  34.             public static final String Name = "summary";
  35.             public static final String DisplayName = "Summary";
  36.             public static final String Uri = "http://compass/toturial/summary";
  37.             public static final String GroupId = "library";
  38.         }
  39.         public static final class Keyword {
  40.             public static final String Id = "keyword";
  41.             public static final String Name = "keyword";
  42.             public static final String DisplayName = "Keyword";
  43.             public static final String Uri = "http://compass/toturial/keyword";
  44.             public static final String GroupId = "library";
  45.         }
  46.         public static final class PublishDate {
  47.             public static final String Id = "publishDate";
  48.             public static final String Name = "publish";
  49.             public static final String DisplayName = "Publish Date";
  50.             public static final String Uri = "http://compass/toturial/publishDate";
  51.             public static final String GroupId = "library";
  52.         }
  53.         public static final class Title {
  54.             public static final String Id = "title";
  55.             public static final String Name = "title";
  56.             public static final String DisplayName = "Title";
  57.             public static final String Uri = "http://compass/toturial/title";
  58.             public static final String GroupId = "library";
  59.         }
  60.         public static final class Type {
  61.             public static final String Id = "type";
  62.             public static final String Name = "type";
  63.             public static final String DisplayName = "Type";
  64.             public static final String Uri = "http://compass/toturial/type";
  65.             public static final String GroupId = "library";
  66.         }
  67.         public static final class TitleName {
  68.             public static final String Id = "titleName";
  69.             public static final String Name = "titleName";
  70.             public static final String DisplayName = "Title Name";
  71.             public static final String Uri = "http://compass/toturial/titleName";
  72.             public static final String GroupId = "library";
  73.         }
  74.         public static final class Isbn {
  75.             public static final String Id = "isbn";
  76.             public static final String Name = "name";
  77.             public static final String DisplayName = "ISBN";
  78.             public static final String Uri = "http://compass/toturial/name";
  79.             public static final String GroupId = "library";
  80.         }
  81.         public static final class Name {
  82.             public static final String Id = "name";
  83.             public static final String Name = "name";
  84.             public static final String DisplayName = "Name";
  85.             public static final String Uri = "http://compass/toturial/name";
  86.             public static final String GroupId = "library";
  87.         }
  88.         public static final class Birthdate {
  89.             public static final String Id = "birthdate";
  90.             public static final String Name = "birthdate";
  91.             public static final String DisplayName = "Birthdate";
  92.             public static final String Uri = "http://compass/toturial/birthdate";
  93.             public static final String GroupId = "library";
  94.             public static final String Format = "yyyy/MM/dd";
  95.         }
  96.         public static final class FirstName {
  97.             public static final String Id = "firstName";
  98.             public static final String Name = "firstName";
  99.             public static final String DisplayName = "First Name";
  100.             public static final String Uri = "http://compass/toturial/firstName";
  101.             public static final String GroupId = "library";
  102.         }
  103.         public static final class LastName {
  104.             public static final String Id = "lastName";
  105.             public static final String Name = "lastName";
  106.             public static final String DisplayName = "Last Name";
  107.             public static final String Uri = "http://compass/sample/library/lastName";
  108.             public static final String GroupId = "library";
  109.         }
  110.     }
  111. }

Author.cpm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE compass-core-mapping PUBLIC
  3.     "-//Compass/Compass Core Mapping DTD 2.0//EN"
  4.     "http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd">
  5. <compass-core-mapping package="com.tutorial">
  6.     <class name="Author" alias="${library.author}">
  7.         <id name="id" />
  8.         <constant>
  9.             <meta-data>${library.type}</meta-data>
  10.             <meta-data-value>${library.type.mdPerson}</meta-data-value>
  11.             <meta-data-value>${library.type.mdAuthor}</meta-data-value>
  12.         </constant>
  13.         <property name="keywords">
  14.             <meta-data boost="2">${library.keyword}</meta-data>
  15.         </property>
  16.         <component name="name" ref-alias="${library.name.md}" />
  17.         <property name="birthdate">
  18.             <meta-data>${library.birthdate}</meta-data>
  19.         </property>
  20.         <reference name="books" ref-alias="${library.book}" />
  21.     </class>
  22.     <class name="Name" alias="${library.name}" root="false">
  23.             <property name="title">
  24.                 <meta-data>${library.titleName}</meta-data>
  25.             </property>
  26.             <property name="firstName">
  27.                 <meta-data>${library.firstName}</meta-data>
  28.                 <meta-data>${library.name}</meta-data>
  29.             </property>
  30.             <property name="lastName">
  31.                 <meta-data>${library.lastName}</meta-data>
  32.                 <meta-data>${library.name}</meta-data>
  33.             </property>
  34.     </class>
  35. </compass-core-mapping>

Book.cpm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE compass-core-mapping PUBLIC 
  3.     "-//Compass/Compass Core Mapping DTD 2.0//EN"
  4.     "http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd">
  5. <compass-core-mapping package="com.tutorial">
  6.     <class name="Book" alias="${library.book}">
  7.     
  8.         <id name="id" />
  9.         <property name="keywords">
  10.             <meta-data boost="2">${library.keyword}</meta-data>
  11.         </property>
  12.         
  13.         <property name="title">
  14.             <meta-data>${library.title}</meta-data>
  15.         </property>
  16.         <property name="publishDate">
  17.             <meta-data>${library.publishDate}</meta-data>
  18.         </property>
  19.         <property name="summary">
  20.             <meta-data>${library.summary}</meta-data>
  21.         </property>
  22.     
  23.     </class>
  24.     
  25. </compass-core-mapping>

library.cmd.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE compass-core-meta-data PUBLIC 
  3.     "-//Compass/Compass Core Meta Data DTD 2.0//EN"
  4.     "http://www.compass-project.org/dtd/compass-core-meta-data-2.0.dtd">
  5. <compass-core-meta-data>
  6.     <meta-data-group id="library" displayName="Library Meta Data">
  7.     
  8.         <description>Library Meta Data</description>       
  9.         <uri>http://compass/toturial</uri>
  10.         
  11.         <alias id="author" displayName="Author">
  12.             <description>Author alias</description>
  13.             <uri>http://compass/toturial/alias/author</uri>
  14.             <name>author</name>
  15.         </alias>
  16.         <alias id="name" displayName="Name">
  17.             <description>Name alias</description>
  18.             <uri>http://compass/toturial/alias/name</uri>
  19.             <name>name</name>
  20.         </alias>
  21.         <alias id="book" displayName="Book">
  22.             <description>Book alias</description>
  23.             <uri>http://compass/toturial/alias/book</uri>
  24.             <name>book</name>
  25.         </alias>
  26.         <meta-data id="type" displayName="Type">
  27.             <description>Type of an entity in the system</description>
  28.             <uri>http://compass/toturial/type</uri>
  29.             <name>type</name>
  30.             <value id="mdPerson">person</value>
  31.             <value id="mdAuthor">author</value>
  32.         </meta-data>
  33.         
  34.         <meta-data id="keyword" displayName="Keyword">
  35.             <description>Keyword associated with an entity</description>
  36.             <uri>http://compass/toturial/keyword</uri>
  37.             <name>keyword</name>
  38.         </meta-data>
  39.         <meta-data id="name" displayName="Name">
  40.             <description>The name of a person (firstName or lastName) without the title</description>
  41.             <uri>http://compass/toturial/name</uri>
  42.             <name>name</name>
  43.         </meta-data>
  44.         <meta-data id="firstName" displayName="First Name">
  45.             <description>The first name of a person</description>
  46.             <uri>http://compass/toturial/firstName</uri>
  47.             <name>firstName</name>
  48.         </meta-data>
  49.         <meta-data id="lastName" displayName="Last Name">
  50.             <description>The last name of a person</description>
  51.             <uri>http://compass/toturial/lastName</uri>
  52.             <name>lastName</name>
  53.         </meta-data>
  54.         <meta-data id="titleName" displayName="Title Name">
  55.             <description>The title of a person</description>
  56.             <uri>http://compass/toturial/titleName</uri>
  57.             <name>titleName</name>
  58.         </meta-data>
  59.         <meta-data id="birthdate" displayName="Birthdate">
  60.             <description>The birthdate of a person</description>
  61.             <uri>http://compass/toturial/birthdate</uri>
  62.             <name format="yyyy/MM/dd">birthdate</name>
  63.         </meta-data>
  64.         
  65.         <meta-data id="isbn" displayName="ISBN">
  66.             <description>ISBN of the book</description>
  67.             <uri>http://compass/toturial/name</uri>
  68.             <name>name</name>
  69.         </meta-data>
  70.         <meta-data id="title" displayName="Title">
  71.             <description>The title of a book or an article</description>
  72.             <uri>http://compass/toturial/title</uri>
  73.             <name>title</name>
  74.         </meta-data>
  75.         <meta-data id="publishDate" displayName="Publish Date">
  76.             <description>The publish date of a book or an article</description>
  77.             <uri>http://compass/toturial/publishDate</uri>
  78.             <name>publish</name>
  79.         </meta-data>
  80.         <meta-data id="summary" displayName="Summary">
  81.             <description>The summary of a book or an article</description>
  82.             <uri>http://compass/toturial/summary</uri>
  83.             <name>summary</name>
  84.         </meta-data>
  85.         
  86.     </meta-data-group>
  87.     
  88. </compass-core-meta-data>

compass.cfg.xml

  1. <!DOCTYPE compass-core-configuration PUBLIC
  2.     "-//Compass/Compass Core Configuration DTD 2.0//EN"
  3.     "http://www.compass-project.org/dtd/compass-core-configuration-2.0.dtd">
  4. <compass-core-configuration>
  5.     <compass>
  6.     
  7.         <setting name="compass.engine.connection">target/index</setting>
  8.         
  9.         <meta-data resource="com/tutorial/library.cmd.xml" />
  10.      
  11.     </compass>
  12. </compass-core-configuration>

测试代码:

LibraryTests.java

  1. package com.demo;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import org.compass.core.Compass;
  5. import org.compass.core.CompassCallbackWithoutResult;
  6. import org.compass.core.CompassException;
  7. import org.compass.core.CompassHits;
  8. import org.compass.core.CompassQueryBuilder;
  9. import org.compass.core.CompassSession;
  10. import org.compass.core.CompassTemplate;
  11. import org.compass.core.CompassTransaction;
  12. import org.compass.core.Resource;
  13. import org.compass.core.config.CompassConfiguration;
  14. import com.tutorial.Author;
  15. import com.tutorial.Book;
  16. import com.tutorial.Identifiable;
  17. import com.tutorial.Library;
  18. import com.tutorial.Name;
  19. public class LibraryTests {
  20.     public static void main(String[] args){
  21.         
  22.         CompassConfiguration config = new CompassConfiguration();
  23.         config.configure("/com/tutorial/compass.cfg.xml");
  24.         config.addClass(Author.class).addClass(Book.class);
  25.         Compass compass = config.buildCompass();
  26.         compass.getSearchEngineIndexManager().deleteIndex();
  27.         compass.getSearchEngineIndexManager().createIndex();
  28.         
  29.         try {
  30.             setUpData(compass);
  31.         } catch (Exception e) {
  32.             e.printStackTrace();
  33.         }
  34.         
  35.         CompassSession session = compass.openSession();
  36.         CompassTransaction tx = null;
  37.         try {
  38.             tx = session.beginTransaction();
  39.             Author author = (Author) session.load(Author.class1);
  40.             
  41.             SimpleDateFormat sdf = new SimpleDateFormat(Library.MetaData.Birthdate.Format);
  42.             System.out.println(sdf.format(author.getBirthdate()));
  43.             System.out.println(author.getName().getFirstName());
  44.             System.out.println(author.getBooks().size());
  45.             
  46.             //delete it
  47.             //session.delete(author);
  48.             
  49.             tx.commit();
  50.         } catch (Exception e) {
  51.             if (tx != null) {
  52.                 tx.rollback();
  53.             }
  54.         } finally {
  55.             session.close();
  56.         }
  57.         
  58.         CompassTemplate compassTemplate = new CompassTemplate(compass);
  59.         compassTemplate.execute(new CompassCallbackWithoutResult() {
  60.             protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
  61.                 String query = "fiercely";
  62.                 CompassHits hits = session.find(query);
  63.                 System.out.println("Found [" + hits.getLength() + "] hits for [" + query + "] query");
  64.                 System.out.println("======================================================");
  65.                 for (int i = 0; i < hits.getLength(); i++) {
  66.                     Object value = hits.data(i);
  67.                     Resource resource = hits.resource(i);
  68.                     System.out.println("ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]");
  69.                     System.out.println(":::: " + value);
  70.                     System.out.println("");
  71.                 }
  72.                 hits.close();
  73.             }
  74.         });
  75.         
  76.         compassTemplate = new CompassTemplate(compass);
  77.         compassTemplate.execute(new CompassCallbackWithoutResult() {
  78.             protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
  79.                 CompassQueryBuilder queryBuilder = session.queryBuilder();
  80.                 CompassHits hits = queryBuilder.bool().addMust(queryBuilder.term("book.id"1))
  81.                 //.addMustNot(queryBuilder.term("author.books.id", 1))
  82.                 .toQuery().hits();
  83.                 
  84.                 System.out.println("Found [" + hits.getLength() + "] hits for [" +    "Jack"  + "] query");
  85.                 System.out.println("======================================================");
  86.                 for (int i = 0; i < hits.getLength(); i++) {
  87.                     Object value = hits.data(i);
  88.                     Resource resource = hits.resource(i);
  89.                     System.out.println("ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]");
  90.                     System.out.println(":::: " + value);
  91.                     System.out.println("");
  92.                 }
  93.                 hits.close();
  94.             }
  95.         });
  96.         
  97.     }
  98.     
  99.     public static void setUpData(Compass compass) throws Exception {
  100.         
  101.         CompassSession session = compass.openSession();
  102.         CompassTransaction tx = session.beginTransaction();
  103.         
  104.         Author jackLondon = new Author();
  105.         jackLondon.setId(new Long(1));
  106.         jackLondon.setName(new Name("Mr""Jack""London"));
  107.         Calendar c = Calendar.getInstance();
  108.         c.set(1876012);
  109.         jackLondon.setBirthdate(c.getTime());
  110.         jackLondon.setKeywords(new String[] { "american author" });
  111.         Book whiteFang = new Book();
  112.         whiteFang.setId(new Long(1));
  113.         whiteFang.setTitle("White Fang");
  114.         c.set(190601);
  115.         whiteFang.setPublishDate(c.getTime());
  116.         whiteFang.setSummary("The remarkable story of a fiercely independent creature of the wild");
  117.         whiteFang.setKeywords(new String[] { "jack london""call of the wild" });
  118.         jackLondon.addBook(whiteFang);
  119.         session.save(whiteFang);
  120.         Book callOfTheWild = new Book();
  121.         callOfTheWild.setId(new Long(2));
  122.         callOfTheWild.setTitle("The Call of the Wild");
  123.         c.set(190301);
  124.         callOfTheWild.setPublishDate(c.getTime());
  125.         callOfTheWild.setSummary("The Call of the Wild is a tale about unbreakable spirit");
  126.         callOfTheWild.setKeywords(new String[] { "jack london""buck""white fang" });
  127.         jackLondon.addBook(callOfTheWild);
  128.         session.save(callOfTheWild);
  129.         session.save(jackLondon);
  130.         
  131.         Author jamesClavell = new Author();
  132.         jamesClavell.setId(new Long(2));
  133.         jamesClavell.setName(new Name("Mr""James""Clavell"));
  134.         c.set(1924910);
  135.         jamesClavell.setBirthdate(c.getTime());
  136.         jamesClavell.setKeywords(new String[] { "far east""shogun""japan""hong kong" });
  137.         Book shogun = new Book();
  138.         shogun.setId(new Long(3));
  139.         shogun.setTitle("Shogun");
  140.         c.set(197501);
  141.         shogun.setPublishDate(c.getTime());
  142.         shogun.setSummary("A story of a hero who is not a person but a place and a time,"
  143.                 + " medieval Japan on the threshold of becoming a sea power");
  144.         shogun.setKeywords(new String[] { "james clavell""Blackthorne""Toranaga""japan" });
  145.         jamesClavell.addBook(shogun);
  146.         session.save(shogun);
  147.         Book taipan = new Book();
  148.         taipan.setId(new Long(4));
  149.         taipan.setTitle("Taipan");
  150.         c.set(196601);
  151.         taipan.setPublishDate(c.getTime());
  152.         taipan.setSummary("Tai-Pan is chinese for /"supreme leader/". This is the man with real power "
  153.                 + "to his hands. And such a Tai-Pan is Dirk Struan who is obsessed by his plan to make Hong Kong "
  154.                 + "the /"jewel in the crown of her British Majesty/". In 1841 he achieves his goal but he has many "
  155.                 + "enemies who try to destroy his plans. Will they succeed?");
  156.         taipan.setKeywords(new String[] { "james clavell""Dirk Struan""joss""hong kong" });
  157.         jamesClavell.addBook(taipan);
  158.         session.save(taipan);
  159.         session.save(jamesClavell);
  160.         
  161.         tx.commit();
  162.         session.close();
  163.     }
  164. }

你可能感兴趣的:(Hibernate,String,null,query,library,books)