Product displayed is the smallest within the group (Criteria)

3

I have a function in the system that I'm developing that returns a batch list where the batch quantity is greater than zero, searching for the product name (foreign key). Until that point I have. When doing the search it returns me all lots of that product that typed the name.

But I have several batches of the same product, I wanted to make a filter so that only the batch is displayed that the validity is the smallest among them, blocking the user's choice to choose a lot of the product, and there is another that validity is smaller and could be released soon.

public List<Lote> lotePorNomeMedicamento(String nome) {
    Session session = this.manager.unwrap(Session.class);

    Criteria criteria = session.createCriteria(Lote.class)
            // fazemos uma associação (join) com medicamento e nomeamos como "m"
            .createAlias("medicamento", "m");

    // acessamos o nome do medicamento associado ao pedido pelo alias "m",
    // criado anteriormente
    criteria.add(Restrictions.ilike("m.nome", nome, MatchMode.ANYWHERE));

    // retringir lotes com quantidade zero
    criteria.add(Restrictions.gt("quantidade",0));

    // restringir lotes para o valor exibido seja o menor do grupo
    criteria.add(Restrictions.gt("validade", value))


    return criteria.addOrder(Order.asc("lote")).list();
}

This is the code I thought of using Restrictions.gt on validity, but the value of these validity values should be the smallest of the three.

Ex:

  

(Lot - Product - Validity - Quantity)

     

Lote01 - Product01 - 02/01/2016 - 10

     

Lot02 - Product01 - 02/03/2016 - 10

     

Lot03 - Product01 - 08/02/2016 - 10

The result that would be displayed would be Lot01 only, because it is lot of the same product that the validity is the smallest among them. Any suggestion? Is it possible to do this using Criteria?

Lot class

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
@Entity
@Table (name = "lote")
public class Lote implements Serializable {

private static final long serialVersionUID = 1L;

private String lote;
private String marca;
private Integer quantidade;
private Date validade;
private Medicamento medicamento;
@SuppressWarnings("unused")
private String lotePesq;

@NotBlank
@Id
public String getLote() {
    return lote;
}

public void setLote(String lote) {
    this.lote = lote;
}

@NotBlank
public String getMarca() {
    return marca;
}

public void setMarca(String marca) {
    this.marca = marca;
}

@Min(value = 0, message = "deve ser maior que zero.")
public Integer getQuantidade() {
    return quantidade;
}

public void setQuantidade(Integer quantidade) {
    this.quantidade = quantidade;
}

@NotNull
public Date getValidade() {
    return validade;
}

public void setValidade(Date validade) {
    this.validade = validade;
}

@NotNull
@ManyToOne
public Medicamento getMedicamento() {
    return medicamento;
}

public void setMedicamento(Medicamento medicamento) {
    this.medicamento = medicamento;
}   

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((lote == null) ? 0 : lote.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Lote other = (Lote) obj;
    if (lote == null) {
        if (other.lote != null)
            return false;
    } else if (!lote.equals(other.lote))
        return false;
    return true;
}

}
    
asked by anonymous 18.03.2016 / 00:35

1 answer

2

Do the following

ProjectionList projections = Projections.projectionList();
projections.add(Projections.min("validade"))
criteria.setProjection(projections)
    
18.03.2016 / 00:52