Commit ba270f8c by Patryk Czarnik

Adnotacje Bean Validation i sprawdzanie podczas save

parent cdac874d
...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
import sklep.utils.ExceptionUtils;
@Controller @Controller
public class ProductController { public class ProductController {
...@@ -66,6 +67,12 @@ public class ProductController { ...@@ -66,6 +67,12 @@ 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.
// Kwestia Bean Validation:
// W tej wersji (bez adnotacji @Valid) Spring wchodzi do tej metody
// i dopiero podczas próby zapisania obiektu operacją save Hibernate
// dokonuje walidacji obiektu względem adnotacji.
// W przypadku niezgodności wyrzucany jest wyjątek, a obiekt nie jest zapisywany.
// Dzieje się to jeszcze zanim dane trafią do bazy danych.
try { try {
System.out.println("Produkt przed zapisem: " + product); System.out.println("Produkt przed zapisem: " + product);
productRepository.save(product); productRepository.save(product);
...@@ -73,7 +80,7 @@ public class ProductController { ...@@ -73,7 +80,7 @@ public class ProductController {
model.addAttribute("saved", true); model.addAttribute("saved", true);
} catch(Exception e) { } catch(Exception e) {
System.out.println("Produkt bez zapisu : " + product); System.out.println("Produkt bez zapisu : " + product);
model.addAttribute("errors", List.of(e.toString())); model.addAttribute("errors", ExceptionUtils.allMessages(e));
} }
return "product_form"; return "product_form";
} }
......
...@@ -2,6 +2,8 @@ package sklep.model; ...@@ -2,6 +2,8 @@ package sklep.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.util.List; import java.util.List;
...@@ -30,6 +32,7 @@ public class Customer implements Serializable { ...@@ -30,6 +32,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
......
...@@ -2,6 +2,8 @@ package sklep.model; ...@@ -2,6 +2,8 @@ package sklep.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Min;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -21,6 +23,7 @@ public class OrderProduct implements Serializable { ...@@ -21,6 +23,7 @@ public class OrderProduct implements Serializable {
@Column(name="actual_price") @Column(name="actual_price")
private BigDecimal actualPrice; private BigDecimal actualPrice;
@Min(1)
private Integer quantity; private Integer quantity;
//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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/** /**
* The persistent class for the products database table. * The persistent class for the products database table.
* *
...@@ -30,13 +41,21 @@ public class Product implements Serializable { ...@@ -30,13 +41,21 @@ public class Product implements Serializable {
@Column(name = "product_id", insertable = false, updatable = false) @Column(name = "product_id", insertable = false, updatable = false)
private Integer productId; private Integer productId;
@Size(max=4000)
private String description; private String description;
@NotNull
@DecimalMin("0.01")
@DecimalMax("9999.99") // tylko dla sprawdzenia działania → docelowo wartość ma być większa
private BigDecimal price; private BigDecimal price;
@Column(name = "product_name") @Column(name = "product_name")
@NotNull
@Size(min=2, max=10) // FIXME
private String productName; private String productName;
@DecimalMin("0.00")
@DecimalMax("0.99")
private BigDecimal vat; private BigDecimal vat;
public Product() { public Product() {
......
package sklep.utils;
import java.util.ArrayList;
import java.util.List;
public class ExceptionUtils {
public static List<String> allMessages(Throwable e) {
List<String> messages = new ArrayList<>();
messages.add(oneMessage(e));
Throwable cause = e.getCause();
while(cause != null) {
messages.add(oneMessage(cause));
cause = cause.getCause();
}
return messages;
}
private static String oneMessage(Throwable e) {
return e.getClass().getSimpleName() + ": " + e.getMessage();
}
}
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