简体   繁体   中英

Java, MongoDB - Embedded document with REST

I'm building a REST service in Java by using MongoDB ( mongodb-java-driver ), Jersey, and Jackson. An example of the classes I'm using is the following: Employee

public class Employee extends BasicDBObject {
    public Employee() {}
    public Employee(String name, Company company) {
        put("name", name);
        put("company", company);
    }
    public String getName() {
        return get("name").toString();
    }
    public Company getCompany() {
        return (Company) get("company");
    }
    public void setCompany(Company company) {
        put("company", company);
    }
}

Company

public class Company extends BasicDBObject {
    public Company() {}
    public Company(String name, String city) {
        put("name", name);
        put("city", city);
    }
    public String getName() {
        return get("name").toString();
    }
    public String getCity() {
        return get("city").toString();
    }
}

and the REST service

public class EmployeeRestSrv {
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createEmployee(Employee employee) {
        DBCollection employeesCollection = DataSource.getDbCollection("employees");
        employeesCollection.setObjectClass(Employee.class);
        // employee.company.name ?
        employeesCollection.insert(employee);
        return Response.ok().build();
    }
}

DataSource is the class that actually creates a connection with MongoDB.

If I send this JSON object with a POST

{
    "employee" : "foo",
    "company" : {
        "name" : "foocompany",
        "city" : "san francisco"
    }
}

I correctly get the document in employeesCollection .

My question is: when in createEmployee at commented line, how can I access the embedded object's fields ( Company )? I also tried employee.get("company").get("name") with no effect.

The final goal is to "force" a sort of One-to-Many relationship and I'd like to be able to store in company document also the list of employees.

Try employee.get("company.name")

That should also work for updating nested documents.

As far as relationships are concerned, this would work as you are storing one-to-many relationships as nested objects.

Try employee.getCompany().getName(); When you say your code "had no effect", what do you mean? (Also, you meant createEmployee , not createUser , right?)

Since BasicDBObject is basically a Map, I implemented this walk-around in Employee class, to obtain the Company structure:

...
public Company getCompany() {
    try {
        return new Company((Map) get("company"));
    } catch(NullPointerException e) {
        return null;
    }
}

public void setCompany(Company c) {
    put("company", c);
}
...

Of course I also created a new constructor in Company, in order to accept Map:

public Company(Map m) {
    super(m);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM