Search code examples
javaoopormmany-to-many

Many to many Relationship in Java classes without using any ORM


Scenario: I am developing an E-Commerce app on android studio. App is consuming web services to get the data. I'm designing light weight classes (just like the DB on my server) to store data that receives from server in JSON format.
Problem: I need to design relationship[Many to Many] between Cart and Product. In DB case I were able to do it by joining tow tables in single one named CartProduct on the basis of their primary keys.

Assumption: Since App will get data from server in JSON format, most probably there will no id related to any entity. So I declared classes without ID. As

Category{
    String name;
    List<Product> products;
} 
Products{
    String name;
    .....
}

So how do I map these CartProducts and OrderProducts tables into classes. Since we have no IDs.


Solution

  • I agree with Amit: if your JSON does not contain any attribute for the entity's identity, you cannot relate to any identy.

    Are the name attributes unique? If so, they could also be used to identify entities. It is still better pratice to make a difference between name and identity. Sometimes there is "Peter"... and then there is another "Peter". Make the difference also in chosing attribute/variable identifiers, if it is an ID, do not call it "name"!