compass小例子出问题?大家帮忙啊
Book类 其中setter、getter方法就不写出来了
public class Book {
private String bookNo;
private String bookName;
private String author;
private String category;
private String contents;
private Double price;
public Book(){
}
}
alias.cmd.xml文件
<?xml version="1.0"?>
<!DOCTYPE compass-core-meta-data PUBLIC
"-//Compass/Compass Core Meta Data DTD 2.0//EN"
"http://www.compass-project.org/dtd/compass-core-meta-data-2.0.dtd">
<compass-core-meta-data>
<meta-data-group id="mybooks" displayName="Mybooks Meta Data">
<description>Book Meta Data</description>
<uri>http://compass/mybooks</uri>
<alias id="book" displayName="Book">
<description>Book alias</description>
<uri>http://compass/mybooks/Book</uri>
<name>book</name>
</alias>
<meta-data id="bookNo" displayName="BookNo">
<description>Author alias</description>
<uri>http://compass/mybooks/bookNo</uri>
<name>bookNo</name>
</meta-data>
<meta-data id="bookName" displayName="BookName">
<description>BookName alias</description>
<uri>http://compass/mybooks/bookName</uri>
<name>bookName</name>
</meta-data>
<meta-data id="author" displayName="Author">
<description>Text alias</description>
<uri>http://compass/mybooks/author</uri>
<name>author</name>
</meta-data>
<meta-data id="category" displayName="Category">
<description>Category alias</description>
<uri>http://compass/mybooks/category</uri>
<name>category</name>
</meta-data>
<meta-data id="contents" displayName="Contents">
<description>Text alias</description>
<uri>http://compass/mybooks/contents</uri>
<name>contents</name>
</meta-data>
<meta-data id="price" displayName="Price">
<description>Price alias</description>
<uri>http://compass/mybooks/price</uri>
<name>price</name>
</meta-data>
</meta-data-group>
</compass-core-meta-data>
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="org.shirdm.compass.document">
<class name="Book" alias="${mybooks.book}">
<id name="bookNo" />
<property name="bookName">
<meta-data>${mybooks.bookName}</meta-data>
</property>
<property name="author">
<meta-data>${mybooks.author}</meta-data>
</property>
<property name="category">
<meta-data>${mybooks.category}</meta-data>
</property>
<property name="contents">
<meta-data>${mybooks.contents}</meta-data>
</property>
<property name="price">
<meta-data>${mybooks.price}</meta-data>
</property>
</class>
</compass-core-mapping>
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="org/shirdm/compass/document/alias.cmd.xml" />
<mapping resource="org/shirdm/compass/document/Book.cpm.xml" />
</compass>
</compass-core-configuration>
测试类
public class MyCompassTest {
private static Compass compass;
static { // 解析Compass配置文件,加载Book实体类
CompassConfiguration config = new CompassConfiguration();
config.configure("/org/shirdm/compass/compass.cfg.xml");
config.addClass(Book.class);
compass = config.buildCompass();
}
private Book addBookA() { // 构造一个Book实例,实际上是构造了一个Document,并向其中添加Field
Book book = new Book();
book.setBookNo("ISBN 987-5-624-16000-8/TP");
book.setBookName("Compass框架技术");
book.setAuthor("飞云居士");
book.setCategory("Java");
String contents = "Compass是封装了Lucene的一个功能强大的框架。"
+ "Compass简单易学,这使得开发者可以像使用Hibernate一样轻松地开发自己的搜索引擎。"
+ "即使对Lucene并不是很熟悉,使用Compass框架开发使你变得游刃有余。";
book.setContents(contents);
book.setPrice(new Double(23.00));
return book;
}
private Book addBookB() {
Book book = new Book();
book.setBookNo("ISBN 388-2-244-16000-9/TP");
book.setBookName("鬼吹灯");
book.setAuthor("本物天下霸唱");
book.setCategory("考古小说类");
String contents = "几位“摸金校尉”(盗墓贼)通过风水秘术在发掘古墓时的诡异经历。"
+ "主人公胡八一祖传的风水秘书残卷,上山下乡时误入大山深处的辽代古墓,"
+ "参军时在昆仑山大冰川发现的九层妖楼,东北中蒙边境的关东军秘密地下...";
book.setContents(contents);
book.setPrice(new Double(58.50));
return book;
}
private void createIndex(Book book) { // 建立索引,与Hibernate很相似
CompassSession session = compass.openSession();
CompassTransaction tx = session.beginTransaction();
session.save(book);
tx.commit();
session.close();
}
private void search(String keyword) { // 根据关键字keyword检索
CompassSession session = compass.openSession();
CompassTransaction tx = session.beginTransaction();
CompassHits hits = session.find(keyword);
System.out.println("检索关键字 " + keyword + " 的检索结果如下:");
System.out.println("********************************************");
for (int i = 0; i < hits.getLength(); i++) {
Book book = (Book) hits.data(i);
System.out.println("书的编号为: " + book.getBookNo());
System.out.println("书的名称为: " + book.getBookName());
System.out.println("书的作者为: " + book.getAuthor());
System.out.println("书的类别为: " + book.getCategory());
System.out.println("书的内容为: " + book.getContents());
System.out.println("书的定价为: " + book.getPrice());
System.out.println("********************************************");
}
System.out.println("共检索出结果 " + hits.length() + " 个。");
hits.close();
tx.commit();
session.close();
compass.close();
}
public static void main(String[] args) {
MyCompassTest myCompass = new MyCompassTest();
myCompass.createIndex(myCompass.addBookA());
myCompass.createIndex(myCompass.addBookB());
myCompass.search("鬼吹灯");
}
}
一测试,报错了
2008-8-1 15:31:05 org.compass.core.config.CompassConfiguration configure
信息: Configuring from resource [/org/shirdm/compass/compass.cfg.xml]
2008-8-1 15:31:05 org.compass.core.config.CompassConfiguration addResource
信息: Mapping resource [org/shirdm/compass/document/alias.cmd.xml] in class loader
2008-8-1 15:31:05 org.compass.core.config.CompassConfiguration addResource
信息: Mapping resource [org/shirdm/compass/document/Book.cpm.xml] in class loader
2008-8-1 15:31:05 org.compass.core.config.builder.DTDConfigurationBuilder processCompass
信息: Configured Compass [default]
java.lang.ExceptionInInitializerError
Caused by: org.compass.core.mapping.MappingException: Compass does not allow multiple aliases for alias [book]
at org.compass.core.mapping.CompassMapping.addMapping(CompassMapping.java:122)
at org.compass.core.config.binding.XmlMappingBinding.doAddConfiguration(XmlMappingBinding.java:140)
at org.compass.core.config.binding.AbstractXmlMappingBinding.doAddInputStream(AbstractXmlMappingBinding.java:42)
at org.compass.core.config.binding.AbstractInputStreamMappingBinding.internalAddInputStream(AbstractInputStreamMappingBinding.java:192)
at org.compass.core.config.binding.AbstractInputStreamMappingBinding.addClass(AbstractInputStreamMappingBinding.java:161)
at org.compass.core.config.CompassMappingBinding.addClass(CompassMappingBinding.java:163)
at org.compass.core.config.CompassConfiguration.addClass(CompassConfiguration.java:446)
at org.shirdm.compass.main.MyCompassTest.<clinit>(MyCompassTest.java:34)
Exception in thread "main"
弄了好久了
都没错啊
不知道哪里错了
各位大虾帮忙啊,谢过先了
[解决办法]
你的配置文件很奇怪
上面有compass/myBooks,还有org.shirdm.compass.document这样的package
我不知道你前面那个链接是怎么来的。。