I need a help to migrate the code with createCriteria for Hibernate 5, the code is this below:
public Curso comDadosIguais(Curso curso) {
return (Curso) this.session.createCriteria(Curso.class)
.add(Restrictions.eq("codigo", curso.getCodigo()))
.uniqueResult();
}
Could you help me?
There are a number of options you can exercise to migrate from the deprecated Hibernate Criteria API, these are just a few that immediately come to mind:
From a HQL / JPQL perspective, you could rewrite your query as a string:
You can also use a
@NamedQuerywhich is basically an annotation you apply to an entity or a package where you supply a HQL query much like you see above, specifying a placeholder for your query parameters much in the same way and then supplying those parameters are runtime when you execute the query, like:And finally, you can also consider the JPA Criteria API. This is available if you're using a JPA
EntityManagerrather than a HibernateSession.