Hi, this is my first post in this forum..I am developing a web app that has an Invoice,CartItem,Customer classes..I need to identify the kind of mapping I need to create.
A user creates cartitems and adds them to ShoppingCart . An Invoice has a Customer and a Set of CartItem objects.I believe cartitem is not dependent upon the invoice's lifetime.So, when an invoice is deleted,the cartitems should not be deleted.
I tried to create the Invoice.hbm.xml and used <set> to associate cartItems to Invoice and decided to not to mention cascade option so that deletion of Invoice does not affect cartitems.
I would like some advice/opinion regarding this..Is this the right way of mapping the associations? I have copied relevant portions of the code below..Please tell me if this is the correct way.
thanks, mark
Invoice.java ========== public class Invoice { private Customer customer; private Set<CartItem> cartItems; private Date invoiceDate; private int invoiceNumber; private boolean isProcessed; public Invoice() { super(); isProcessed = false; cartItems = new HashSet<CartItem>(); } ... } CartItem.java ======== public class CartItem { private Product product; private int quantity; ... }
Invoice.hbm.xml ========== ... <class name="Invoice" table="INVOICE"> <id name="invoice_id" column="INVOICE_ID" type="long"> <generator class="native"/> </id> <property name="invoiceDate" type="date" column="INVOICE_DATE" /> <property name="invoiceNumber" type="int" column="INVOICE_NUMBER" unique="true"/> <property name="isProcessed" type="boolean" column="IS_PROCESSED" /> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID" lazy="false" /> <!--when an invoice is deleted the cart items should not be deleted --> <set name="cartItems" table="CARTITEM" lazy="false" > <key column="INVOICE_ID" /> <one-to-many class="CartItem" /> </set> </class> ...
|