Commit a13e42f5 by Patryk Czarnik

Adnotacje Bean Validation

parent c2da37f9
...@@ -92,6 +92,7 @@ public class ProductController { ...@@ -92,6 +92,7 @@ public class ProductController {
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute) // Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// i nie trzeba dodawać go w osobnym poleceniu. // i nie trzeba dodawać go w osobnym poleceniu.
try { 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); productRepository.save(product);
model.addAttribute("saved", true); model.addAttribute("saved", true);
} catch(Exception e) { } catch(Exception e) {
......
...@@ -2,6 +2,9 @@ package sklep.model; ...@@ -2,6 +2,9 @@ package sklep.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.Pattern;
import java.util.List; import java.util.List;
...@@ -17,6 +20,7 @@ public class Customer implements Serializable { ...@@ -17,6 +20,7 @@ public class Customer implements Serializable {
@Id @Id
@Column(name="customer_email") @Column(name="customer_email")
@Email
private String customerEmail; private String customerEmail;
private String address; private String address;
...@@ -29,6 +33,7 @@ public class Customer implements Serializable { ...@@ -29,6 +33,7 @@ public class Customer implements Serializable {
private String phoneNumber; private String phoneNumber;
@Column(name="postal_code") @Column(name="postal_code")
@Pattern(regexp="\\d{2}-\\d{3}")
private String postalCode; private String postalCode;
//bi-directional many-to-one association to Order //bi-directional many-to-one association to Order
......
package sklep.model; package sklep.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal; 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. * The persistent class for the order_products database table.
...@@ -24,6 +32,7 @@ public class OrderProduct implements Serializable { ...@@ -24,6 +32,7 @@ public class OrderProduct implements Serializable {
@Column(name="actual_vat") @Column(name="actual_vat")
private BigDecimal actualVat; private BigDecimal actualVat;
@Min(1)
private Integer quantity; private Integer quantity;
//bi-directional many-to-one association to Order //bi-directional many-to-one association to Order
......
...@@ -2,6 +2,11 @@ package sklep.model; ...@@ -2,6 +2,11 @@ package sklep.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*; 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; import java.math.BigDecimal;
/** /**
...@@ -21,11 +26,15 @@ public class Product implements Serializable { ...@@ -21,11 +26,15 @@ public class Product implements Serializable {
private String description; private String description;
@DecimalMin("0.01")
private BigDecimal price; private BigDecimal price;
@Column(name="product_name") @Column(name="product_name")
@NotNull
@Size(min=3, max=10)
private String productName; private String productName;
@Min(0)
private BigDecimal vat; private BigDecimal vat;
public Product() { 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