📄 orderitem.java
字号:
/*
* Copyright 2006 Borys Burnayev
*
* 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 com.rdacorp.petstore.domain;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;
/**
* @author Borys Burnayev
*/
@Entity
@Table(name = "order_item")
@SequenceGenerator(name = "OrderItemSeq", sequenceName = "sq_order_item")
public class OrderItem {
private Integer orderItemId;
private long quantity;
private BigDecimal extendedPrice;
private Order order;
private Item item;
private int version;
@Column(name = "row_version")
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public OrderItem(long quantity, Order order, Item item) {
this.quantity = quantity;
this.order = order;
this.item = item;
updateExtendedPrice();
}
public OrderItem(long quantity, Item item) {
this.quantity = quantity;
this.item = item;
updateExtendedPrice();
}
public OrderItem() {
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "OrderItemSeq")
@Column(name = "order_item_sysid")
public Integer getOrderItemId() {
return this.orderItemId;
}
public void setOrderItemId(Integer orderItemId) {
this.orderItemId = orderItemId;
}
@Column(name = "quantity")
public long getQuantity() {
return this.quantity;
}
public void setQuantity(long quantity) {
this.quantity = quantity;
updateExtendedPrice();
}
@Column(name = "extended_price")
public BigDecimal getExtendedPrice() {
return this.extendedPrice;
}
public void setExtendedPrice(BigDecimal extendedPrice) {
this.extendedPrice = extendedPrice;
}
@ManyToOne
@JoinColumn(name = "order_summary_sysid")
public Order getOrder() {
return this.order;
}
public void setOrder(Order orderSummary) {
this.order = orderSummary;
}
@ManyToOne
@JoinColumn(name = "item_sysid")
public Item getItem() {
return this.item;
}
public void setItem(Item item) {
this.item = item;
}
public String toString() {
String id = orderItemId != null ? orderItemId.toString() : "<no ID>";
return "Order Item " + id + ":\nQuantity: " + quantity + "\nExtended Price: " + extendedPrice + "\n" + order
+ item;
}
public boolean equals(Object object) {
if (!(object instanceof OrderItem))
return false;
OrderItem orderItem = (OrderItem) object;
return item.equals(orderItem.item);
}
public int hashCode() {
return item.hashCode();
}
public void increaseQuantity(long quantity) {
updateQuantity(quantity);
}
public void decreaseQuantity(long quantity) {
updateQuantity(-quantity);
}
public void updateQuantity(long quantity) {
this.quantity += quantity;
updateExtendedPrice();
}
public void updateExtendedPrice() {
if (item != null)
extendedPrice = item.getPrice().multiply(new BigDecimal(this.quantity));
else
extendedPrice = new BigDecimal(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -