Ektorp - @DocumentReference无效

时间:2016-03-11 16:12:46

标签: couchdb ektorp

我是Couch的新手,我觉得懒惰有趣。所以我试着这样做,但可能是因为我做错了,它不起作用,我无法弄清楚出了什么问题。我提到http://ektorp.org/reference_documentation.html#d100e394。我收到以下错误。 (我能够单独阅读文档,但不能通过文档引用来阅读)。任何帮助将非常感激。我正在使用ektorp版本1.4.1。

09:51:03.581 [main] DEBUG org.ektorp.impl.StdCouchDbConnector - Querying CouchDb view at /employee/_design/Employee/_view/ektorp_docrefs_addresses?startkey=%5B%22222%22%2C%22addresses%22%5D&endkey=%5B%22222%22%2C%22addresses%22%2C%7B%7D%5D&include_docs=true.
Exception in thread "main" org.ektorp.DocumentNotFoundException: nothing found on db path: /employee/_design/Employee/_view/ektorp_docrefs_addresses?startkey=%5B%22222%22%2C%22addresses%22%5D&endkey=%5B%22222%22%2C%22addresses%22%2C%7B%7D%5D&include_docs=true, Response body: {"error":"not_found","reason":"missing"}
    at org.ektorp.http.StdResponseHandler.createDbAccessException(StdResponseHandler.java:40)
    at org.ektorp.http.StdResponseHandler.error(StdResponseHandler.java:68)
    at org.ektorp.http.RestTemplate.handleResponse(RestTemplate.java:110)
    at org.ektorp.http.RestTemplate.getUncached(RestTemplate.java:27)
    at org.ektorp.impl.StdCouchDbConnector.executeQuery(StdCouchDbConnector.java:431)
    at org.ektorp.impl.StdCouchDbConnector.queryView(StdCouchDbConnector.java:423)
    at org.ektorp.impl.docref.ViewBasedCollection.loadSetResult(ViewBasedCollection.java:69)
    at org.ektorp.impl.docref.ViewBasedCollection.loadFromBackReferences(ViewBasedCollection.java:50)
    at org.ektorp.impl.docref.ViewBasedCollection.initialize(ViewBasedCollection.java:102)
    at 
org.ektorp.impl.docref.LazyLoadingViewBasedCollection.invoke(LazyLoadingViewBasedCollection.java:27)
    at com.sun.proxy.$Proxy13.size(Unknown Source)
    at com.poc.main.Main.main(Main.java:37)

我的测试文件如下。

员工文档

{
   "_id": "222",
   "_rev": "1-32d4d52d4ea11b521b1e366959ce5557",
   "name": "Mariamma",
   "age": 21
}

地址文档

{
   "_id": "ad1",
   "_rev": "2-39f3162e0fc63ed5526adda5ae496291",
   "employeeId": 222,
   "addressLine1": "555 Randolph",
   "city": "Chicago"
}

Employee.java

public class Employee extends CouchDbDocument {
  private String name;

  private int age;

  @DocumentReferences(backReference = "employeeId", fetch = FetchType.LAZY, cascade=CascadeType.NONE)
  private Set<Address> addresses = new HashSet<Address>();
// getters and setters
}

Address.java

public class Address extends CouchDbDocument {

  private String employeeId; 

  private String addressLine1;

  private String addressLine2;

  private String city;

  private String State;

  private String zipCode;
// getters and setters
}

EmployeeDao.java

public class EmployeeDao {

  private final CouchDbConnector db;

  public EmployeeDao() {
    db = getEmployeeDatabaseConnector();
  }

  public CouchDbConnector getEmployeeDatabaseConnector() {
    HttpClient httpClient = new StdHttpClient.Builder().build();
    CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);
    CouchDbConnector connector = new StdCouchDbConnector("employee", couchDbInstance);
    connector.createDatabaseIfNotExists();
    return connector;
  }

  public Employee read(String employeeId) {
    return this.db.get(Employee.class, employeeId);
  }
}

Main.java

public class Main {

  public static void main(String[] args) {
    EmployeeDao dao = new EmployeeDao();
    Employee employee = dao.read("222");
    System.out.println(employee.getName());
    System.out.println(employee.getAddresses().size()); // this line throws error.
  }

}

1 个答案:

答案 0 :(得分:2)

从堆栈跟踪

... Response body: {"error":"not_found","reason":"missing"}

这表明视图/_design/Employee/_view/ektorp_docrefs_addresses不存在。

<强>更新

Chapter 6 of the Ektorp Reference Docs谈论视图生成。看起来你自己并没有创造它并且没有告诉Ektorp为你做这件事。

您应该通过扩展Employee然后在其上调用CouchDbRepositorySupport来查看为initStandardDesignDocument()创建存储库类。