是否可以在Ektorp中定义一对一关联?在Ektorp文档中,我只发现了如何将对象列表嵌入到外键引用的对象中。 我想制作一份文件:
{
name: "John",
surname: "Smith",
address: {
street: "Abc Street",
zip: "12345",
city: "My City"
}
}
由于
答案 0 :(得分:1)
嵌入这样的完整文档非常友好。一种直接的方法是定义一个具有Contact
类的Address
类。
// Contact.java
package com.example;
public class Contact extends org.ektorp.support.CouchDbDocument {
private String name;
private String surname;
private Address address;
//... constructors and/or setters
public String getName() { return name; }
public String getSurname() { return surname; }
public Address getAddress() { return address; }
}
// Address.java
package com.example;
public class Address {
private String street;
private String zip;
private String city;
//... constructors and/or setters
public String getStreet() { return street; }
public String getZip() { return zip; }
public String getCity() { return city; }
}
当写入数据库时,它将生成一个如下所示的文档:
{
"_id": "590ede48b04e7c61914c677f0f000b57",
"_rev": "1-fae3423a6ea58741dca18764a39e48a9",
"name": "John",
"surname": "Smith",
"address": {
"street": "Abc Street",
"zip": "12345",
"city": "My City"
}
}