简体   繁体   中英

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. 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. 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
  • Store describes the store
  • ProductOffer describes where the product is offered and for how much

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> . Or the ProductOffer may link to both Product and Store .

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. in that case the HashMap is quite handy, at the cost of memory of course. 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.

More information on your use case would help in understanding your situation better.

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