Commit bdeb79cb by Patryk Czarnik

Klasy modelu dla sklepu

parent d2e054d3
...@@ -25,6 +25,10 @@ ...@@ -25,6 +25,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
......
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.util.List;
/**
* The persistent class for the customers database table.
*
*/
@Entity
@Table(name="customers")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="customer_email")
private String customerEmail;
private String address;
private String city;
@Column(name="customer_name")
private String customerName;
@Column(name="phone_number")
private String phoneNumber;
@Column(name="postal_code")
@Pattern(regexp="\\d{2}-\\d{3}")
private String postalCode;
//bi-directional many-to-one association to Order
@OneToMany(mappedBy="customer")
private List<Order> orders;
public Customer() {
}
public String getCustomerEmail() {
return this.customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getCustomerName() {
return this.customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPostalCode() {
return this.postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public List<Order> getOrders() {
return this.orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public Order addOrder(Order order) {
getOrders().add(order);
order.setCustomer(this);
return order;
}
public Order removeOrder(Order order) {
getOrders().remove(order);
order.setCustomer(null);
return order;
}
}
\ No newline at end of file
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.sql.Timestamp;
import java.util.List;
/**
* The persistent class for the orders database table.
*
*/
@Entity
@Table(name="orders")
@NamedQuery(name="Order.findAll", query="SELECT o FROM Order o")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="order_id", insertable=false, updatable=false)
private Integer orderId;
@Temporal(TemporalType.DATE)
@Column(name="delivery_date")
private Date deliveryDate;
@Column(name="order_date")
private Timestamp orderDate;
private String status;
//bi-directional many-to-one association to OrderProduct
@OneToMany(mappedBy="order")
private List<OrderProduct> orderProducts;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name="customer_email")
private Customer customer;
public Order() {
}
public Integer getOrderId() {
return this.orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public Date getDeliveryDate() {
return this.deliveryDate;
}
public void setDeliveryDate(Date deliveryDate) {
this.deliveryDate = deliveryDate;
}
public Timestamp getOrderDate() {
return this.orderDate;
}
public void setOrderDate(Timestamp orderDate) {
this.orderDate = orderDate;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
public List<OrderProduct> getOrderProducts() {
return this.orderProducts;
}
public void setOrderProducts(List<OrderProduct> orderProducts) {
this.orderProducts = orderProducts;
}
public OrderProduct addOrderProduct(OrderProduct orderProduct) {
getOrderProducts().add(orderProduct);
orderProduct.setOrder(this);
return orderProduct;
}
public OrderProduct removeOrderProduct(OrderProduct orderProduct) {
getOrderProducts().remove(orderProduct);
orderProduct.setOrder(null);
return orderProduct;
}
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
\ No newline at end of file
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Min;
import java.math.BigDecimal;
/**
* The persistent class for the order_products database table.
*
*/
@Entity
@Table(name="order_products")
@NamedQuery(name="OrderProduct.findAll", query="SELECT o FROM OrderProduct o")
public class OrderProduct implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private OrderProductPK id;
@Column(name="actual_price")
private BigDecimal actualPrice;
@Min(1)
private Integer quantity;
//bi-directional many-to-one association to Order
@ManyToOne
@JoinColumn(name="order_id", insertable=false, updatable=false)
private Order order;
//uni-directional many-to-one association to Product
@ManyToOne
@JoinColumn(name="product_id", insertable=false, updatable=false)
private Product product;
public OrderProduct() {
}
public OrderProductPK getId() {
return this.id;
}
public void setId(OrderProductPK id) {
this.id = id;
}
public BigDecimal getActualPrice() {
return this.actualPrice;
}
public void setActualPrice(BigDecimal actualPrice) {
this.actualPrice = actualPrice;
}
public Integer getQuantity() {
return this.quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Order getOrder() {
return this.order;
}
public void setOrder(Order order) {
this.order = order;
}
public Product getProduct() {
return this.product;
}
public void setProduct(Product product) {
this.product = product;
}
}
\ No newline at end of file
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The primary key class for the order_products database table.
*
*/
@Embeddable
public class OrderProductPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="order_id", insertable=false, updatable=false)
private Integer orderId;
@Column(name="product_id", insertable=false, updatable=false)
private Integer productId;
public OrderProductPK() {
}
public Integer getOrderId() {
return this.orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public Integer getProductId() {
return this.productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof OrderProductPK)) {
return false;
}
OrderProductPK castOther = (OrderProductPK)other;
return
this.orderId.equals(castOther.orderId)
&& this.productId.equals(castOther.productId);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.orderId.hashCode();
hash = hash * prime + this.productId.hashCode();
return hash;
}
}
\ No newline at end of file
package sklep.model;
import java.io.Serializable;
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.
*
*/
/*
* Sposób na zapisanie kilku named queries w starszych wersjach Javy (tylko nie wiem jakich ;-) )
*
* @NamedQueries({
* @NamedQuery(name="Product.findAll", query="SELECT p FROM Product p"),
* @NamedQuery(name="Product.sredniaCena", query="SELECT avg(p.price) FROM Product p"),
* })
*/
@Entity
@Table(name = "products")
@NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p")
@NamedQuery(name = "Product.sredniaCena", query = "SELECT avg(p.price) FROM Product p")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
// W celu obserwacji powstawania i usuwania obiektów:
// { System.out.println("!new Product!"); }
// @Override
// protected void finalize() {
// System.out.println("Ginie Produkt nr " + productId);
// }
// Używanie finalize tak, że miałoby wpływać na działanie, jest niezalecane!
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id", insertable = false, updatable = false)
private Integer productId;
@Size(max=4000)
private String description;
@NotNull
@DecimalMin("0.01")
@DecimalMax("99999999.99")
private BigDecimal price;
@Column(name = "product_name")
@NotNull
@Size(min=2, max=100)
private String productName;
@DecimalMin("0.00")
@DecimalMax("0.99")
private BigDecimal vat;
public Product() {
}
public Integer getProductId() {
return this.productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getProductName() {
return this.productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getVat() {
return this.vat;
}
public void setVat(BigDecimal vat) {
this.vat = vat;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", description=" + description + ", price=" + price
+ ", productName=" + productName + ", vat=" + vat + "]";
}
}
\ No newline at end of file
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Customer;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, String> {
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Order;
@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
}
package sklep.repository;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Product;
/* Gdy w projekcie umieścimy interfejs rozszerzający interfejs JpaRepository (albo podobny)
* i oznaczymy go adnotacją @Repository (albo skonfigurujemy w inny sposób...),
* to Spring AUTOMATYCZNIE UTWORZY IMPLEMENTACJĘ tego interfejsu.
* Dzięki temu "za darmo" mamy metody dający podstawowy dostęp do tabel.
* Dodatkowo w interfejsie można dopisać własne metody, w których nazwie kryje się zasada działania.
* Np. findByPriceBetween szuka produktów o cenie między min i max.
*
* findByEmail - szuka rekordów z polem email równym parametrowi.
*
* https://www.baeldung.com/spring-data-derived-queries
*/
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
List<Product> findByProductName(String name);
List<Product> findByProductNameContains(String name);
List<Product> findByProductNameContainingIgnoringCase(String name);
List<Product> findByPriceBetween(BigDecimal min, BigDecimal max);
List<Product> findByProductNameContainingIgnoringCaseAndPriceBetween(String name, BigDecimal min, BigDecimal max);
}
spring.datasource.url=jdbc:postgresql://localhost/sklep
spring.datasource.username=kurs
spring.datasource.password=abc123
alx.photo_dir=/home/patryk/sklep/foto
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