I am a newbee to Couch and I found lazy-fetching interesting. So I was trying that out but probably due to something wrong I did, it doesn't work and I can't figure out what is wrong. I referred http://ektorp.org/reference_documentation.html#d100e394. I get the error below. (I am able to read the docs separately but not by document referencing). Any help would be much appreciated. I'm using ektorp version 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)
My test files are below.
Employee doc
{
"_id": "222",
"_rev": "1-32d4d52d4ea11b521b1e366959ce5557",
"name": "Mariamma",
"age": 21
}
Address doc
{
"_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.
}
}
From your stack trace
... Response body: {"error":"not_found","reason":"missing"}
This suggests that the view /_design/Employee/_view/ektorp_docrefs_addresses
isn't there.
Update
Chapter 6 of the Ektorp Reference Docs talks about view generation. It looks like you are not created it yourself and have not told Ektorp to do it for you.
You should look at creating a repository class for Employee
by extending CouchDbRepositorySupport
and then calling initStandardDesignDocument()
on it.