Промежуточное сохранение.

This commit is contained in:
Programmist73 2023-03-27 17:51:24 +04:00
parent 438e1ed4d4
commit 9297fb06ae

View File

@ -1,7 +1,9 @@
package labWork.premium_store.model; package labWork.premium_store.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
@Entity @Entity
public class Tank { public class Tank {
@ -20,18 +22,23 @@ public class Tank {
@JoinColumn(name = "level_id") @JoinColumn(name = "level_id")
private Level level; private Level level;
@Column(nullable = false)
private int cost;
//реализация двунаправленной связи мнокие-ко-многим
@ManyToMany @ManyToMany
@JoinTable(name = "tanks_of_clients", @JoinTable(name = "tanks_of_clients",
joinColumns = @JoinColumn(name = "tank_fk"), joinColumns = @JoinColumn(name = "tank_fk"),
inverseJoinColumns = @JoinColumn(name = "client_fk")) inverseJoinColumns = @JoinColumn(name = "client_fk"))
private List<Client> clients; private List<Client> clients;
@Column(nullable = false)
private int cost;
public Tank() { public Tank() {
} }
public Long getId(){
return id;
}
public Tank(String name, int cost) { public Tank(String name, int cost) {
this.name = name; this.name = name;
this.cost = cost; this.cost = cost;
@ -53,35 +60,57 @@ public class Tank {
this.cost = cost; this.cost = cost;
} }
public List<OrderProducts> getProducts() { public Level getLevel() {
return products; return level;
} }
public void setProducts(List<OrderProducts> products) { public void setLevel(Level level) {
this.products = products; this.level = level;
} }
public void addProduct(OrderProducts orderProducts){ public Nation getNation() {
if (products == null){ return nation;
this.products = new ArrayList<>(); }
public void setNation(Nation nation) {
this.nation = nation;
}
public List<Client> getClients() {
return clients;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
public void addClient(Client newClient){
if (clients == null){
this.clients = new ArrayList<>();
} }
if (!products.contains(orderProducts))
this.products.add(orderProducts); if (!clients.contains(newClient))
this.clients.add(newClient);
} }
public void removeProducts(OrderProducts orderProducts){
if (products.contains(orderProducts)) public void removeClient(Client remClient){
this.products.remove(orderProducts); if (clients.contains(remClient))
this.clients.remove(remClient);
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof Order order)) return false;
return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate()) && Objects.equals(getPrice(), order.getPrice()); if (!(o instanceof Tank tank)) return false;
return Objects.equals(getId(), tank.getId()) && Objects.equals(getName(), tank.getName())
&& Objects.equals(getCost(), tank.getCost()) && Objects.equals(getClients(), tank.getClients())
&& Objects.equals(getLevel(), tank.getLevel());
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(getId(), getDate(), getPrice()); return Objects.hash(getId(), getName(), getLevel(), getClients(), getCost());
} }
} }