Doubt about Hibernate Criteria

Asked

Viewed 65 times

1

I am trying to make a system for service order just for me to train. In the service orders I can have several categories. And my doubt is time to display, for example, a ranking of the most used categories for a period x using the criteria of Hibernate. Something that the result was organized like this:

Category A - 10 times

Category B - 8 times
Category C - 5 times

What can I use in this case?

Classes are briefly mapped like this

OrderService class

@SuppressWarnings("serial")
@Entity
@Table(name = "ordem_servico")
public class OrdemServico extends GenericModel {

@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "data_os", nullable = false)
private Date dataOS;   

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
@JoinTable(name = "os_categoria", joinColumns = @JoinColumn(name = "id_os"),inverseJoinColumns = @JoinColumn(name = "id_categoria"))
private List<Categoria> categorias;     
}

Class Category

@SuppressWarnings("serial")
@Entity
@Table(name = "categoria_os")
public class Categoria extends GenericModel {
@Column(name = "nome_cat", nullable = false, length = 40)

@NotBlank
private String nome;

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

}
  • Explain better the ai business rule. It is missing a field for the period as well.

  • @Sorry to keep you waiting. Type I have service orders that can have multiple categories and I wanted to make like a top 10 of most used categories

  • Each service order or all?

  • @Matheussilva of all service categories. Type one ranking

1 answer

0


The way I thought to solve it was like this, must have better k

Create a table that represents the Manytomany link:

@Entity
public class OrdemServicoCategoria {

@Id
@GeneratedValue
private Long idOsCategoria;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idOrdem")
private OrdemServico os;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="nome")
private Categoria categoria; //Atributo nome vira ID na tabela categoria
//...Outros métodos
}

In order service has the attribute

@OneToMany(mappedBy = "os", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<OrdemServicoCategoria> osCategorias = new ArrayList<OrdemServicoCategoria>();

A Non-persistent Ranking class, just to get the data:

public class Ranking {

private Categoria categoria;
private Long vezes; 


public Ranking(Categoria categoria, Long vezes) {
    this.categoria = categoria;
    this.vezes = vezes;     
}

//Outros métodos
}

The DAO method to recover is like this:

public List<Ranking> getRanking() {
        String consulta = "select new org.autocomplete.models.Ranking(os_cat.categoria, COUNT(idOsCategoria))"
                + " FROM OrdemServicoCategoria os_cat group by os_cat.categoria";
        TypedQuery<Ranking> query = manager.createQuery(consulta, Ranking.class);       
        return query.getResultList();       
    }

In this case, I didn’t use Criteria, I don’t know what your need to use criteria, with criteria would look like this link https://stackoverflow.com/questions/8491796/hibernate-group-by-criteria-object, but I couldn’t solve with this k

You can place the date in the intermediate table and use the Where clause for the periods.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.