简体   繁体   English

在Java中映射多对多关联的最佳方法

[英]Best way to map many-to-many association in Java

I was wondering what is the best way to map a manyToMany association in JAVA. 我想知道在JAVA中映射manyToMany关联的最佳方法是什么。 I have three tables describes like this: 我有三个表描述如下:

+----------+    +-----------------+    +----------+ 
| products |    | products_stores |    | stores   | 
+----------+    +-----------------+    +----------+ 
| barecode |    | #barecode       |    | storeID  | 
| name     |----| #storeID        |----| location | 
+----------+    | price           |    +----------+ 
                +-----------------+    

I was thniking about two ways to reprensent it in JAVA. 我有两种方式在JAVA中重申它。 And I would like to know which one is better in performance or anything. 而且我想知道哪一个在性能或任何方面都更好。

First way: 第一种方式:

    public class Product {

        private String name;
        private String barecode;
        private double price;
        private Store store;

        public Product(String name, String barecode, double price, Store store){/*...*/}
        //getter/setter
    }

    public class Store {

            private String name;
            private String location;

            public Store(String name, String location){/*...*/}
            //getter/setter
    }

Second way: 第二种方式:

    public class Product {

        private String name;
        private String barecode;
        private HashMap<Store, double> priceList;

        public Product(String name, String barecode, HashMap<Store, double> priceList){//}
        //getter/setter
    }

    public class Store {

            private String name;
            private String location;

            public Store(String name, String location){/*...*/}
            //getter/setter
    }

My first thought was the first way, but I also thought about the second way, and I am wondering which one is better, if any. 我的第一个想法是第一种方式,但我也想到了第二种方式,我想知道哪一个更好,如果有的话。 Or if there is another way better than those two. 或者,如果有另外一种方式比那两种更好。

Thank you. 谢谢。

I would go with three objects, just as you have three DB tables: 我会使用三个对象,就像你有三个数据库表一样:

  • Product describes the product independent on where it's sold Product描述的产品与销售地点无关
  • Store describes the store Store描述商店
  • ProductOffer describes where the product is offered and for how much ProductOffer描述了产品的提供位置和数量

Depending on your use case, list of ProductOffer s may be part of Store or Product , or there can be an independent container that manages the relations between products and stores using eg a multimap Product -> List<ProductOffer> . 根据您的使用案例, ProductOffer列表可能是StoreProduct一部分,或者可以有一个独立的容器来管理产品和商店之间的关系,例如使用multimap Product - > List<ProductOffer> Or the ProductOffer may link to both Product and Store . 或者ProductOffer可以链接到ProductStore

The argument for making a class for the third class is extensibility - it can easily cater for you wanting to consider product availability in the store, for example. 为第三类创建类的论点是可扩展性 - 例如,它可以很容易地满足您想要考虑商店中的产品可用性。

depends on what you intend to do with the data. 取决于您打算如何处理数据。 for example, the second way is better if you are looking to perform a lot of querying operations by the Store object. 例如,如果您希望Store对象执行大量查询操作,第二种方法会更好。 in that case the HashMap is quite handy, at the cost of memory of course. 在这种情况下,HashMap非常方便,当然会以内存为代价。 However, if your main concern is storage and there is not too much overlap in the Store class, then the first way is better as you can just create a List of Product objects and keep adding to it. 但是,如果您主要关注的是存储并且Store类中没有太多重叠,那么第一种方法更好,因为您可以创建一个Product对象列表并继续添加它。

More information on your use case would help in understanding your situation better. 有关您的用例的更多信息将有助于更好地了解您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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