Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.clinimuni.vaccinaeweb.domain;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Objects;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.validation.constraints.NotNull;
- /**
- * @author ivocalado
- *
- */
- @Entity(name = "CalendarioVacinal")
- @Table(name = "calendario_vacinal")
- public class CalendarioVacinal {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Integer id;
- @NotNull
- @Column(unique = true)
- private String nome;
- @Column(length = 1500)
- private String descricao;
- @OneToMany(mappedBy = "calendario", cascade = CascadeType.ALL, orphanRemoval = true)
- private List<VacinaCalendarioVacinal> vacinas = new ArrayList<VacinaCalendarioVacinal>();
- public void addVacina(Vacina vacina, Set<Integer> doses) {
- VacinaCalendarioVacinal entrada = new VacinaCalendarioVacinal(vacina, this);
- entrada.setDosesAplicacao(doses);
- vacina.getCalendariosAssociados().add(entrada);
- }
- /**
- * Remove interligação entre vacinas e o calendário. Solução adaptada de @see
- * https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/
- */
- public void removeVacina(Vacina vacina) {
- for (Iterator<VacinaCalendarioVacinal> iterator = vacinas.iterator(); iterator.hasNext();) {
- VacinaCalendarioVacinal entrada = iterator.next();
- if (entrada.getCalendario().equals(this) && entrada.getVacina().equals(vacina)) {
- iterator.remove();
- entrada.getVacina().getCalendariosAssociados().remove(entrada);
- entrada.setCalendario(null);
- entrada.setVacina(null);
- }
- }
- }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- return Objects.hash(nome);
- }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (!(obj instanceof CalendarioVacinal)) {
- return false;
- }
- CalendarioVacinal other = (CalendarioVacinal) obj;
- return Objects.equals(nome, other.nome);
- }
- /**
- * @return the id
- */
- public Integer getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(Integer id) {
- this.id = id;
- }
- /**
- * @return the nome
- */
- public String getNome() {
- return nome;
- }
- /**
- * @param nome the nome to set
- */
- public void setNome(String nome) {
- this.nome = nome;
- }
- /**
- * @return the vacinas
- */
- public List<VacinaCalendarioVacinal> getVacinas() {
- return vacinas;
- }
- /**
- * @param vacinas the vacinas to set
- */
- public void setVacinas(List<VacinaCalendarioVacinal> vacinas) {
- this.vacinas = vacinas;
- }
- /**
- * @return the descricao
- */
- public String getDescricao() {
- return descricao;
- }
- /**
- * @param descricao the descricao to set
- */
- public void setDescricao(String descricao) {
- this.descricao = descricao;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement