View Javadoc
1   package ejava.jpa.example.validation;
2   
3   import java.math.BigDecimal;
4   import java.text.NumberFormat;
5   
6   import javax.persistence.*;
7   import javax.validation.constraints.Min;
8   import javax.validation.constraints.NotNull;
9   import javax.validation.constraints.Size;
10  
11  @Entity
12  @Table(name="VALIDATION_PURCHASEITEM")
13  public class PurchaseItem {
14  	@Id @GeneratedValue
15  	private int id;
16  	
17  	@Column(length=20, nullable=false)
18  	@NotNull(message="item description is required")
19  	@Size(max=20, message="description too large")
20  	private String description;
21  	
22  	@Column(precision=7, scale=2, nullable=false)
23  	@NotNull(message="item amount is required")
24  	private BigDecimal amount;
25  	
26  	@Column(nullable=false)
27  	@Min(value=1, message="item count is required")
28  	private int count;
29  
30  	public int getId() { return id; }
31  
32  	public String getDescription() { return description; }
33  	public PurchaseItem setDescription(String description) {
34  		this.description = description;
35  		return this;
36  	}
37  
38  	public BigDecimal getAmount() { return amount; }
39  	public PurchaseItem setAmount(BigDecimal amount) {
40  		this.amount = amount;
41  		return this;
42  	}
43  
44  	public int getCount() { return count; }
45  	public PurchaseItem setCount(int count) {
46  		this.count = count;
47  		return this;
48  	}
49  	
50  	public BigDecimal getTotal() {
51  		return amount.multiply(new BigDecimal(count));
52  	}
53  	
54  	@Override
55  	public String toString() {
56  		NumberFormat nf = NumberFormat.getCurrencyInstance();
57  		return String.format("%20s %dx %d", description, count, nf.format(getTotal()));
58  	}
59  }