Java类中的多对多关系,而不使用任何ORM

时间:2016-09-01 05:50:38

标签: java oop orm many-to-many

场景:我正在android studio上开发一个电子商务应用。应用程序正在使用Web服务来获取数据。我正在设计轻量级类(就像我服务器上的DB一样)来存储从服务器以JSON格式接收的数据 问题:我需要设计Cart和Product之间的关系[多对多]。 在数据库的情况下,我可以通过基于主键在名为CartProduct的单个表中连接两个表来实现。

假设:由于App将以JSON格式从服务器获取数据,因此很可能不存在与任何实体相关的id。所以我宣布没有ID的课程。 如

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

那么如何将这些CartProductsOrderProducts表映射到类中。由于我们没有身份证。

2 个答案:

答案 0 :(得分:1)

If i understand correct, you stuck to map json with db tables, I think, you have to add some unique column, key would be preferred, otherwise you would be able to get whole data, but in case, to request any action on a single row you can stuck, there would be no identity to identify your row.

答案 1 :(得分:1)

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"!