Using Retrofit and GreenDao with nested json objects
I want to combine Retrofit and GreenDao, but I have a problem with nested Json-Objects. My nested fields are left blank.
This is Json DataStructure
[
{
"id": 1,
"street": "Streetname",
"zipcode": 12345,
"city": "MyCity",
"phone_number": "+123456789",
"position": "12.0000, 9.0000",
"company": {
"title": "CompanyName",
"group": {
"title": "GroupName"
}
}
}
]
My DaoGenerator looks like this
Entity customItem = schema.addEntity("CustomItems");
customItem.addIdProperty();
customItem.addStringProperty("street");
customItem.addIntProperty("zipcode");
customItem.addStringProperty("city");
customItem.addStringProperty("phone_number");
customItem.addStringProperty("position");
Entity company = schema.addEntity("Company");
company.addIdProperty();
company.addStringProperty("title");
Entity group = schema.addEntity("Group");
group.addIdProperty();
group.addStringProperty("title");
Property companyPropId = customItem.addLongProperty("companyId").notNull().getProperty();
customItem.addToOne(company, companyPropId);
Property groupPropId = company.addLongProperty("groupId").notNull().getProperty();
company.addToOne(group, groupPropId);
My problem is customItem.getCompany () returns null, but the "id" values ββin "position" are accurate. I'm not sure what the problem is, as my CustomItem class contains a member
private Company company;
and an installer for the company and I don't see any typo.
public void setCompany(Company company) {
if (company == null) {
throw new DaoException("To-one property 'companyId' has not-null constraint; cannot set to-one to null");
}
synchronized (this) {
this.company = company;
companyId = company.getId();
company__resolvedKey = companyId;
}
}
source to share
I ran it, but I had several problems.
1) When I wanted to save CustomItem, Company and Group I, there was a problem with getters getCompany () and getGroup () returning null, because they do not return the item directly, but retrieve it from the DB. So I added a recipient to the generated CustomItem object class that just returns a company member. Now I was able to insert the company into the db. Getter looks like this:
// KEEP METHODS - put your custom methods here
public Company getCompanyLocal() {
return company;
}
// KEEP METHODS END
The same work for the Company and the Group. But there was another problem ...
2) The second problem was that the group "Group" as "group" is a reserved SQL keyword. I see one solution and bad solution to this problem:
-
The good thing is to change your json data from "group" to ie "business_group" and change your DAOs accordingly. Done.
-
Bad workaround, if you are in the same situation as me where you cannot change the json you can do the following. I don't save the group at all, but I can access it through the company. It appears there somehow. So I added a getter to my company class, for example the CustomItem getter. It works, but you should avoid it. As you cannot query your DB for group or load group from db.
source to share
To solve your second problem, add this code to your DAO generator:
beacon.addStringProperty("business_group"); //Daogenerator
And add this code to your network manager:
//add this into your network manager
FieldNamingStrategy strategy = new FieldNamingStrategy() {
@Override
public String translateName(Field field) {
if(field.getName().equalsIgnoreCase("business_group")) {
return "group";
} else {
return field.getName();
}
}
};
And set this property for your Gson:
//add this in your Gson
.setFieldNamingStrategy(strategy)
hope this helps!
source to share