Commit a13e42f5 by Patryk Czarnik

Adnotacje Bean Validation

parent c2da37f9
......@@ -92,6 +92,7 @@ public class ProductController {
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// i nie trzeba dodawać go w osobnym poleceniu.
try {
// Gdy próbujemy wywołać save, a obiekt nie spełnia wymagań validation, to wtedy Hibernate zablokuje taki zapis (wyrzuci wyjątek).
productRepository.save(product);
model.addAttribute("saved", true);
} catch(Exception e) {
......
......@@ -2,6 +2,9 @@ package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.Pattern;
import java.util.List;
......@@ -17,6 +20,7 @@ public class Customer implements Serializable {
@Id
@Column(name="customer_email")
@Email
private String customerEmail;
private String address;
......@@ -29,6 +33,7 @@ public class Customer implements Serializable {
private String phoneNumber;
@Column(name="postal_code")
@Pattern(regexp="\\d{2}-\\d{3}")
private String postalCode;
//bi-directional many-to-one association to Order
......
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Min;
/**
* The persistent class for the order_products database table.
......@@ -24,6 +32,7 @@ public class OrderProduct implements Serializable {
@Column(name="actual_vat")
private BigDecimal actualVat;
@Min(1)
private Integer quantity;
//bi-directional many-to-one association to Order
......
......@@ -2,6 +2,11 @@ package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
/**
......@@ -21,11 +26,15 @@ public class Product implements Serializable {
private String description;
@DecimalMin("0.01")
private BigDecimal price;
@Column(name="product_name")
@NotNull
@Size(min=3, max=10)
private String productName;
@Min(0)
private BigDecimal vat;
public Product() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment