java - Collections in POJOs -
i generating pojos database using different tools , noticed generate collections fields, getters , setters, 1 many relationships , others didn't.
let's have order , product table. each order can have 1 or many products.
collection<product> list = new arraylist<>(); list.add(product1); list.add(product2);
method 1:
order order = new order(); order.setdate(...); orderdao.add(order); orderdao.addproductbatch(list)
method 2:
order order = new order(); order.setdate(...); order.setproductcollection(list); orderdao.add(order);
and in add method, include addproductbatch call.
which method prefered? 1 many relationships adding multiple objects in single transaction never occurs - in case wouldn't need of these collections - correct?
it depends on implementation of dao...
in method 2, build order , products in business model, pass complete , consistent order (order + list of products) saved dao. transaction implementation internal dao.
in method 1, call twice dao, first order (without product), again list of products related order. means, either dao stateful , have method execute transaction when done setting up, or there 2 transactions. if in case of last option, consistency of db can wrong (having order without products).
method 2 better since allows stateless dao, , clean transaction management.
Comments
Post a Comment