如何解决自动创建视图
我正在尝试使用Ektorp库从ouchDB中查询数据。我在initStandardDesignDocument()处得到空指针异常; 如果我在mydatabase中手动创建视图,则代码运行良好。但是无法通过代码自动创建视图。
请在下面找到我的代码:
主类:
package com.example.couchPoC;
import java.net.MalformedURLException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws MalformedURLException {
SpringApplication.run(DemoApplication.class,args);
CouchConnector connect = new CouchConnector();
connect.newRepoView();
System.out.println("finish");
}
}
实体类:沙发:
package com.example.couchPoC;
import org.ektorp.support.Typediscriminator;
import com.fasterxml.jackson.annotation.JsonProperty;
@Typediscriminator
public class Sofa {
private String id;
private String revision;
private String color;
@JsonProperty("_id")
public String getId() {
return id;
}
@JsonProperty("_id")
public void setId(String s) {
id = s;
}
@JsonProperty("_rev")
public String getRevision() {
return revision;
}
@JsonProperty("_rev")
public void setRevision(String s) {
revision = s;
}
public void setColor(String s) {
color = s;
}
public String getColor() {
return color;
}
}
存储库类:
package com.example.couchPoC;
import java.util.List;
import org.ektorp.CouchDbConnector;
import org.ektorp.ViewQuery;
import org.ektorp.support.CouchDbRepositorySupport;
import org.ektorp.support.GenerateView;
import org.ektorp.support.View;
public class SofarepositoryView extends CouchDbRepositorySupport<Sofa> {
String designDocName;
{
initStandardDesignDocument();
}
public SofarepositoryView(Class<Sofa> type,CouchDbConnector db,String designDocName) {
super(type,db,designDocName);
this.designDocName = "_design/" + designDocName;
}
@Override
@View( name="all",map = "function(doc) { if (doc.title) { emit(doc.dateCreated,doc._id) } }")
public List<Sofa> getAll() {
return queryView("all");
}
@GenerateView
public List<Sofa> findByColor(String color) {
return queryView("by_color",color);
}
}
CouchConnector.java:
package com.example.couchPoC;
import java.net.MalformedURLException;
import java.util.List;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.NameConventions;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
public class CouchConnector {
HttpClient httpClient;
CouchDbInstance dbInstance;
CouchDbConnector db;
CouchConnector() throws MalformedURLException
{
httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin").password("admin")
.build();
dbInstance = new StdCouchDbInstance(httpClient);
db = new StdCouchDbConnector("mydatabase",dbInstance);
db.createDatabaseIfNotExists();
String stdDesignDocumentId = NameConventions.designDocName("_design/Sofa");
System.out.println();
Sofa newSofa = new Sofa();
newSofa.setColor("black");
newSofa.setId("sofa1");
db.create(newSofa);
Sofa newSofa1 = new Sofa();
newSofa1.setColor("blue");
newSofa1.setId("sofa2");
db.create(newSofa1);
Sofa sofa = db.get(Sofa.class,"sofa1");
sofa.setColor("white");
db.update(sofa);
}
public void newRepoView()
{
SofarepositoryView viewSofas = new SofarepositoryView(Sofa.class,"Sofa");
List<Sofa> allSofas1= viewSofas.getAll();
System.out.println("all sofas in DB using view");
System.out.println(allSofas1.size());
System.out.println(allSofas1);
System.out.println(allSofas1.get(0).getId());
for(int i=0;i<allSofas1.size();i++)
{
System.out.println(allSofas1.get(i).getId()+" - "+allSofas1.get(i).getColor());
}
List<Sofa> filterSofa = viewSofas.findByColor("blue");
System.out.println("filtered sofas in DB using view");
for(int i=0;i<allSofas1.size();i++)
{
System.out.println(filterSofa.get(i).getId()+" - "+filterSofa.get(i).getColor());
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。