Java tutorial
/* * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.bjfu.springdao.jpa.domain.order; import java.math.BigDecimal; import java.util.Collections; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import org.springframework.util.Assert; import cn.bjfu.springdao.jpa.domain.AbstractEntity; import cn.bjfu.springdao.jpa.domain.Address; import cn.bjfu.springdao.jpa.domain.Customer; /** * An order. * * @author Oliver Gierke */ @Entity @Table(name = "Orders") public class Order extends AbstractEntity { @ManyToOne(optional = false) private Customer customer; @ManyToOne private Address billingAddress; @ManyToOne(optional = false, cascade = CascadeType.ALL) private Address shippingAddress; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "order_id") private Set<LineItem> lineItems = new HashSet<LineItem>(); /** * Creates a new {@link Order} for the given {@link Customer}. * * @param customer must not be {@literal null}. * @param shippingAddress must not be {@literal null}. */ public Order(Customer customer, Address shippingAddress) { this(customer, shippingAddress, null); } /** * Creates a new {@link Order} for the given customer, shipping and billing {@link Address}. * * @param customer must not be {@literal null}. * @param shippingAddress must not be {@literal null}. * @param billingAddress can be {@@iteral null}. */ public Order(Customer customer, Address shippingAddress, Address billingAddress) { Assert.notNull(customer); Assert.notNull(shippingAddress); this.customer = customer; this.shippingAddress = shippingAddress.getCopy(); this.billingAddress = billingAddress == null ? null : billingAddress.getCopy(); } protected Order() { } /** * Adds the given {@link LineItem} to the {@link Order}. * * @param lineItem */ public void add(LineItem lineItem) { this.lineItems.add(lineItem); } /** * Returns the {@link Customer} who placed the {@link Order}. * * @return */ public Customer getCustomer() { return customer; } /** * Returns the billing {@link Address} for this order. * * @return */ public Address getBillingAddress() { return billingAddress != null ? billingAddress : shippingAddress; } /** * Returns the shipping {@link Address} for this order; * * @return */ public Address getShippingAddress() { return shippingAddress; } /** * Returns all {@link LineItem}s currently belonging to the {@link Order}. * * @return */ public Set<LineItem> getLineItems() { return Collections.unmodifiableSet(lineItems); } /** * Returns the total of the {@link Order}. * * @return */ public BigDecimal getTotal() { BigDecimal total = BigDecimal.ZERO; for (LineItem item : lineItems) { total = total.add(item.getTotal()); } return total; } }