Mapping JPA ListMap ..

6

I have the following problem and would like some opinion to know the best approach:

Let's suppose that you have a system that manages a school and would like to have the option for the administrator of the system in which it can register a modality / lesson that is and link prices according to the days of the week available for that class .

For example:

  • JPA class is available for MONDAY, FOURTH and SIXTH for 50 reais, or THIRD and FIFTH for 35 reais;
  • JSF class is available from MONDAY to FRIDAY for 80 reais, or TUESDAY, FOURTH and FIFTH for 40 reais.

That is, give the option to the administrator to create a price according to the days of the week. What would be the best option for JPA mapping?

Initially I created a Enum with every day of the week and thought about creating something like List<Map<List<DiasDaSemana>, Double>> precos , somewhat confusing right?

Does anyone have any idea how best to solve this, and if that is an acceptable way to map what kind of mapping to do? Through @ElementCollection ? Any help is welcome!

    
asked by anonymous 06.02.2015 / 19:26

2 answers

0

I had the idea of creating a map for the options like this:

Map<String, Map<String,Double>> opções = new HashMap();

This way you can specify the courses in the external map and the internal map, specifying the day of the week and the price.

    
06.02.2015 / 23:52
0

I would do it this way:

import java.math.BigDecimal;
import java.time.DayOfWeek;

public class Curso {
    private int id;
    private String nome;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
}

public class Preco {
    private int id;
    // já tem no Java, não tem porque criar outro enum
    private DayOfWeek diaDaSemana; 
    // nunca trabalhe com valores financeiros usando Double ou Float a perda de precisão pode te causar problemas, especialmente em relatários
    private BigDecimal preco;
    private Curso curso;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public DayOfWeek getDiaDaSemana() {
        return diaDaSemana;
    }
    public void setDiaDaSemana(DayOfWeek diaDaSemana) {
        this.diaDaSemana = diaDaSemana;
    }
    public BigDecimal getPreco() {
        return preco;
    }
    public void setPreco(BigDecimal preco) {
        this.preco = preco;
    }
    public Curso getCurso() {
        return curso;
    }
    public void setCurso(Curso curso) {
        this.curso = curso;
    } 
}

To access, use:

List<Preco> precos;
    
05.05.2018 / 06:11