Ektorp - @DocumentReference 不工作

Ektorp - @DocumentReference not working

我是 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.
  }

}

来自您的堆栈跟踪

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

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

更新

Chapter 6 of the Ektorp Reference Docs 谈论视图生成。看起来不是您自己创建的,也没有告诉 Ektorp 为您创建。

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