package com.project.beautycenter.model; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import javax.persistence.*; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import static java.lang.Float.NaN; @Entity @Table(name = "uslugi",schema = "project") public class Uslugi { @Id @Column(name = "usluga_id", nullable = false, length = 100) private String id; @Column(name = "dejnost", length = 100) private String dejnost; @Column(name = "cena", nullable = false, length = 100) private String cena; @OneToMany(mappedBy = "uslugi",cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private Set ocenas = new LinkedHashSet<>(); public Uslugi(){} public Uslugi(String id, String dejnost, String cena) { this.id = id; this.dejnost = dejnost; this.cena = cena; } public Set getOcenas() { return ocenas; } public void setOcenas(Set ocenas) { this.ocenas = ocenas; } public Float getAvgRating(){ List oceni = oceniToList(); float sum = 0; float num = 0; for (Ocena x : oceni){ if(x.getUslugi().id.equals(this.id)){ sum+= x.getVrednost(); num++; } } float avg = sum/num; return avg; } public List oceniToList(){ List oceni = new ArrayList(ocenas.size()); for (Ocena x : ocenas) oceni.add(x); return oceni; } @PreRemove private void removeUslugaOdOcena() { for (Ocena o : oceniToList()) { o.getUslugi().oceniToList().remove(this); } } public List removeOcenaOdUsluga(Ocena o){ oceniToList().remove(o); return oceniToList(); } public String getCena() { return cena; } public void setCena(String cena) { this.cena = cena; } public String getDejnost() { return dejnost; } public void setDejnost(String dejnost) { this.dejnost = dejnost; } public String getId() { return id; } public void setId(String id) { this.id = id; } }