EasyCriteria – Utilizando a Criteria do JPA de um modo simples

Olá, tudo bem?

Vamos ver hoje sobre essa ferramenta que facilita o uso da Criteria do JPA que deixa o código bem limpo, fácil de utilizar e portável pelas implementações do JPA.

Ao final desse post se encontra disponível o código fonte para download.

O que é uma Criteria? É atualmente a melhor solução para consultas que são criadas de modo dinâmicos. Imagine uma tela onde se tem diversas opções de pesquisa. Pode ser por nome, por idade ou até mesmo pelos dois. Veja como ficaria a consulta caso fosse utilizado uma concatenação de String:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";

if(parameters[0].equals("name")){
	hql += " and p.name = '" + values[0] + "'";
}

if(parameters[1].equals("age")){
	hql += " and p.age = " + values[1];
}

TypedQuery<Person> query = em.createQuery(hql, Person.class);

System.out.println(query.getResultList());

Repare que no código acima é necessário fazer a concatenação de String o que pode levar ao famoso ataque hacker “SQL Injection”. A solução para evitar esse tipo de ataque seria utilizando query com parâmetros e não concatenação:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";

if(parameters.contains("name")){
	hql += " and p.name = :name";
}

if(parameters.contains("age")){
	hql += " and p.age = :age";
}

TypedQuery<Person> query = em.createQuery(hql, Person.class);

if(parameters.contains("name")){
	query.setParameter("name", values[0].toString());
}

if(parameters.contains("age")){
	query.setParameter("age", Integer.valueOf(values[1].toString()));
}

System.out.println(query.getResultList());

Note que o problema do SQL Injection foi resolvido mas agora o código deve verificar os parâmetros na hora de construir a query q na hora de popular os valores; o ato de popular os parâmetros acabam por gerar uma tarefa extra.

O pessoal da Java teve a brilhante ideia de criar o conceito de Criteria que é perfeita para esse tipo de utilização. Veja abaixo como ficaria o mesmo código utilizando a Criteria nativa do JPA:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);

if(parameters.contains("name")){
	Path<String> name = root.get("name");
	cq.where(cb.and(cb.equal(name, values[0])));
}

if(parameters.contains("age")){
	Path<Integer> name = root.get("age");
	cq.where(cb.and(cb.equal(name, Integer.valueOf(values[1].toString()))));
}

TypedQuery<Person> query = em.createQuery(cq);

System.out.println(query.getResultList());

Veja como ficou prático a passagem de parâmetros já com seu valor. Não existe a necessidade de concatenar string, ou muito menos de ficar populando parâmetros depois que a query estiver pronta.

Infelizmente essa api do Criteria ficou muito complexa e demasiadamente verbosa. Para fazer apenas um “select p from Person p” a criteria abaixo seria necessária:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);

TypedQuery<Person> query = em.createQuery(cq);
System.out.println(query.getResultList());

É muito código para uma função muito simples, listar todas as pessoas de uma tabela.

Para evitar toda essa verbosidade é que foi criado o projeto Open Source chamado EasyCriteria.

O EasyCriteria é simples, prático e tem uma abordagem de “esconder” toda a verbosidade facilitando o uso da Criteria do JPA. Utilizando o EasyCriteria a consulta acima ficaria assim:

EntityManager em = emf.createEntityManager();
EasyCriteria<Person> easyCriteria = EasyCriteriaFactory.createQueryCriteria(em, Person.class);

if(parameters.contains("name")){
	easyCriteria.whereEquals("name", values[0]);
}

if(parameters.contains("age")){
	easyCriteria.whereEquals("age", values[1]);
}

System.out.println(easyCriteria.getResultList());

Note que toda a verbosidade do JPA foi embora. Agora é possível ter um código simples para a construção de um query dinâmica. Sobre o código acima vale a pena destacar:

  • Linha 2: Uma instância do EasyCriteria é criada através de uma “factory”. Essa factory existe para abstrair todos os passos necessários para a criação de um objeto do tipo EasyCriteriaImp. Nas versões futuras virão outros tipos de EasyCriteria virão a exemplo do Tuple.

  • Linhas 5 e 9: A passagem de parâmetros ficou mais objetiva. Agora para se passar um parâmetro para comparar valores (“name = :name”) basta utilizar o método equals que espera como primeiro parâmetro o nome do atributo da classe; e como segundo o parâmetro o valor a ser comparado.

  • Linha 12: Para realizar a consulta não é mais necessário a utilização da interface Query. O próprio EasyCriteria toma conta dessa função. É possível extrair diretamente o resultado da query pelo EasyCriteria. Existem dois métodos que fazem isso atualmente o “EasyCriteria.getSingleResult()” e o “EasyCriteria.getResultList()”.

No site do EasyCriteria é possível encontrar diversos exemplos e os métodos que podem ser utilizados. Outra vantagem de se utilizar o EasyCriteria é que todos os seus comandos podem ser “linkados”:

easyCriteria.whereEquals("name", values[0]).whereEquals("age", values[1]).getResultList();

É uma bilioteca que está leve pois a única dependência é o JPA que o sistema já terá que ter configurado. Atenção: é necessário que sua aplicação já tenha o JPA rodando com alguma implementação.

Essa biblioteca foi desenvolvido utilizando JUnit e testado com Hibernate, OpenJPA e EclipseLink. O JUnit também utiliza do framework Cobertura para verificar se todo o código está sendo testado, atualmente 100% do código está coberto.

O EasyCriteria se encontra na versão beta e já tem em vista novas releases com novas funcionalidades.

Outra vantagem do EasyCriteria é que seu código não fica acoplado à alguma implementação. Hoje o Hibernate tem uma ferramenta de criteria muito boa e prática, mas você tem que ficar “preso” a ele. Com o EasyCriteria você poderá utilizar qualquer implementação. A prova disso é que os testes do EasyCriteria são realizados com as implementações citadas agora a pouco.

E EasyCriteria facilita funções como in, like, empty para a criteria do JPA. Além de realizar join (simples, sem adição de parâmetros), distinct e até order by tudo por criteria.

Aqui você encontra o EasyCriteria para download e para acesso a toda sua documentação (http://easycriteria.uaihebert.com).

Clique aqui para fazer o download do código fonte do post de hoje.

Espero que esse post/ferramenta possa te ajudar.

Qualquer dúvida/questionamento/opnião basta falar.

Até a próxima! \o_

EasyCriteria – An easy way to use the JPA Criteria

Hello, how are you?

Today we will see about this tool that make easier to use the JPA Criteria. The application that uses this library will be cleaner, easier to use and portable across the JPA implementations.

At the end of this post you will find the source code to download.

What is Criteria? Currently is the best solution to create dynamic queries. Imagine a page that allows the user to do several types of queries; the requested query could be by name, by age or with both. Take a look bellow in how the query would look like if we concatenate a String:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";

if(parameters[0].equals("name")){
	hql += " and p.name = '" + values[0] + "'";
}

if(parameters[1].equals("age")){
	hql += " and p.age = " + values[1];
}

TypedQuery<Person> query = em.createQuery(hql, Person.class);

System.out.println(query.getResultList());

Notice that in the code above a String concatenation is made; remember that this practice is a bad and dangerous practice because it allows “SQL Injection” hacker attack. To avoid this attack we should use a query with parameters:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";

if(parameters.contains("name")){
	hql += " and p.name = :name";
}

if(parameters.contains("age")){
	hql += " and p.age = :age";
}

TypedQuery<Person> query = em.createQuery(hql, Person.class);

if(parameters.contains("name")){
	query.setParameter("name", values[0].toString());
}

if(parameters.contains("age")){
	query.setParameter("age", Integer.valueOf(values[1].toString()));
}

System.out.println(query.getResultList());

Notice that the SQL Injection problem were solved but now the code must check parameters to add it to the query and later to pass its values; the code needs of two “parameters searches” to complete the task.

The Java/Oracle developers had the brilliant idea when they created the Criteria concept that is perfect to this kind of situation. Check bellow how the code would look like with the native JPA Criteria:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);

if(parameters.contains("name")){
	Path<String> name = root.get("name");
	cq.where(cb.and(cb.equal(name, values[0])));
}

if(parameters.contains("age")){
	Path<Integer> name = root.get("age");
	cq.where(cb.and(cb.equal(name, Integer.valueOf(values[1].toString()))));
}

TypedQuery<Person> query = em.createQuery(cq);

System.out.println(query.getResultList());

Is possible to see that to pass the parameters values is easier. There is no need to concatenate the String or to check the parameters list values to populate the values.

Unfortunately the Criteria API is to complex and verbose to the extreme. If you want to do only a “select p from Person p” you would need to create the criteria bellow:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);

TypedQuery<Person> query = em.createQuery(cq);
System.out.println(query.getResultList());

Is to much code to do something so easy, list all persons from a table.

To avoid all this verbosity the Open Source project named EasyCriteria were created. If a developer uses the EasyCriteria the query above would look like bellow:

EntityManager em = emf.createEntityManager();
EasyCriteria<Person> easyCriteria = EasyCriteriaFactory.createQueryCriteria(em, Person.class);

if(parameters.contains("name")){
	easyCriteria.whereEquals("name", values[0]);
}

if(parameters.contains("age")){
	easyCriteria.whereEquals("age", values[1]);
}

System.out.println(easyCriteria.getResultList());

Notice that the all JPA verbosity is gone. Now it is possible to have a clean code, easier to create dynamic queries. About the code above is worth to talk about:

  • Line 2: An instance of the EasyCriteria is created through a “factory”. This factory exists to do the abstraction of the every needed steep to create an object of the EasyCriteriaImp type. In the future verions new types of the EasyCriteria will be added, e.g. “Tuple”.
  • Lines 5 and 9: It is easier to pass the parameters. To pass the parameters to compare values (“name = :name”) just use the equals method that take as first parameter the attribute name; the second parameter will be the value that will be equaled.
  • Line 12: To run the query it will not be necessary to use the Query interface. The EasyCriteria itself takes this responsibility. It is possible to extract the query result through the EasyCriteria. There are two methods available to get the query result: EasyCriteria.getSingleResult(), EasyCriteria.getResultList().

In the EasyCriteria web page it is available several code samples and the methods that can be used. Other advantage of the EasyCriteria is the ability to “link” all methods:

easyCriteria.whereEquals("name", values[0]).whereEquals("age", values[1]).getResultList();

It is an light weight library because the only dependency is the JPA that the system will need to have. Attention: your application will need to have a JPA implementation up and running.

This library was developed with JUnit and tested with Hibernate, OpenJPA and EclipseLink. The JUnit also uses the Cobertura framework to check if all code lines (or most of it) are covered by the tests, so far we got 100% of coverage.

EasyCriteria still in Beta but the development team already got planed some releases and functionalities.

Other EasyCriteria advantage is that your software code in no long “coupled” to any kind of JPA implementation. Today the Hibernate has a good criteria tool, but your code must stay “attached” to it. With the EasyCriteria you will be able to use any kind of JPA implementation. The proof of this decoupled library is that the EasyCriteria has been tested with 3 implementations quoted earlier.

The EasyCriteria has the methods: in, like, empty and others. The developer will be able to do join (just simple joins without parameter), distinct or even order by all with Criteria.

Here you will find the EasyCriteria to download and have access to all its documentation.

Click here to download the source code of this post.

I hope that this post/tool may help you.

If you have any doubt/question/comment just post it.

See you soon! \o_

Como agendar sua certificação Java – Oracle

Olá, tudo bem?

No post de hoje veremos como agendar sua prova de certificação Java pela PearsonVUE.

São diversas imagens exibindo como agendar sua prova e o material que é recebido depois da prova de certificação da Oracle para Java.

Na segunda página se encontram passo a passo do agendamento da prova com seus devidos comentários.

E na última página será exibido o material que chega, caso o profissional seja aprovado.

How to schedule your Java – Oracle certification

Hello, how are you?

Today we will see how to schedule your Java certification exam by the PearsonVUE.

There are several images displaying how to schedule your exam and the certification material that you will receive after you pass the test.

At the second page you will find the “how to” schedule your test.

In the last page you will find the certification material that you will receive after you pass the test.

Como testar sua JPQL / HQL sem realizar Deploy

Olá, tudo bem?

Você já teve a vontade de testar sua HQL / JPQL sem precisar realizar o deploy de sua aplicação?

A solução apresentada aqui hoje é simples e funciona para qualquer implementação do JPA: Hibernate, OpenJPA, EclipseLink dentre outras.

O código de hoje pode ser executado mesmo que você não tenha banco de dados. Ele utiliza o HSQLDB e já tem uma massa de dados pronta para ser consultada.

A fonte original desse código pode ser encontrada no livro: “Pro JPA 2: Mastering the Java™ Persistence API – Mike Keith, Merrick Schincariol“. O post de hoje acrescenta ao código original: parâmetros para as consultas e o teste de NamedQueries.

O que será apresentado no post de hoje:

  • Página 02: Classes do Modelo e Geração dos dados
  • Página 03: Classe de teste abstrata
  • Página 04: Teste de Query dinâmica
  • Página 05: Teste de NamedQuery
  • Página 06: Execução
  • Página 07: Adaptando o código para sua aplicação
  • Página 08: Sugestões

Ao final desse post se encontra o código fonte para download.

Na página 07 serão destacadas as partes dos códigos que necessitarão de alterações para que você possa aplicar o código do post de hoje ao seu projeto.

How to test your JPQL / HQL without a Deploy

Hello, how are you?

Have you ever wanted to test your JPQL / HQL without doing a full deploy of your application?

What we will see here today is simple solution that works for any JPA implementation: Hibernate, OpenJPA, EclipseLink and others.

The base source code found in this post came from this book: “Pro JPA 2: Mastering the Java™ Persistence API – Mike Keith, Merrick Schincariol”. This post will add to the original code: query parameters and NamedQuery test.

The post contents:

  • Page 02: Model classes and data Generation
  • Page 03: Abstract test class
  • Page 04: Dynamic Query test
  • Page 05: NamedQuery test
  • Page 06: Running the application
  • Page 07: Using this post code with your application code
  • Page 08: Proposals

You will find the source code of this post in the last page.

In the page 07 you will see how to apply the code of this post to the code of your application.

Aplicação Web Completa Tomcat JSF Primefaces JPA Hibernate

Olá, tudo bem?

Nesse post comemorativo de 100.000 visitas será descrita uma aplicação web completa. O post de hoje utilizará as seguintes tecnologias: Tomcat7, JSF 2 (Facelets e Libraries) com Primefaces (utilizando AutoComplete), JPA / Hibernate (com relacionamento NxN), Login por Filter.

Caso você queira ver uma aplicação completa utilizando JSF + EJB + JPA + JAAS + JBoss 7 clique aqui.

Para o projeto de hoje você precisará dos artefatos abaixo (todos os JARs você encontra com o código fonte disponível na última página):

O banco utilizado foi o Postgres, mas você pode utilizar qualquer um. Basta alterar o driver e a url de conexão do arquivo persistence.xml.

Ao final desse post você irá encontrar o código fonte juntamente com as bibliotecas utilizadas no projeto.

O que será visto em cada página:

  • Página 02: Modelo mapeado com anotações JPA. Utilização do relacionamento NxN (@ManyToMany) , NamedQueries utilizando Join Fetch e Enum como atributo.
  • Página 03: DAO Genérico, controle de transação, utilizar métodos genéricos que preencham os parâmetros de uma query.
  • Página 04: Façades utilizando transações, utilizar método findReferenceOnly, cuidados ao utilizar o comando entityManager.merge().
  • Página 05: Filters.
  • Página 06: ManagedBeans. Injetar um ManagedBean dentro de outro, observações sobre @ViewScoped.
  • Página 07: JSFMessageUtil.
  • Página 08: Arquivos de configurações: log4j.properties, messages.properties.
  • Página 09: xhtml pages, Facelets.
  • Página 10: Primefaces AutoComplete, JSF Converter utilizando “forClass”.
  • Página 11: Facilitando o uso de CSS/javascript/imagens com JSF.
  • Página 12: Configurações “web.xml”.
  • Página 13: Práticas para um programa mais seguro.
  • Página 14: Executando a aplicação

A aplicação de hoje terá um cadastro de pessoas e cachorros onde apenas o ADMIN poderá cadastrar os cachorros. Antes de executar o projeto crie o banco de dados: “JSFCrudDB”.

Full Web Application with Tomcat JSF Primefaces JPA Hibernate

Hello, how are you?

We are happy for the 100k visitors. To celebrate this mark in this blog we created this post that will show how to create a full web application using the following tools: Tomcat7, JSF2 (Facelets and Libraries) with Primefaces (with AutoComplete), JPA / Hibernate (with a relationship NxN), Login with Filter.

If you want to see a full web application with JSF + EJB + JPA + JBoss 7 click here.

To run the code of this post you will need the artifacts bellow (all jar file you will find in the last page of this post):

You can use the any database you want, you will need the specific driver of the database and to edit the URL connection at the persistence.xml.

At the end of this post you will find the source code with all needed libraries to download.

What you will see today in this post:

  • Page 02: Entity classes of the model. A relationship NxN (@ManyToMany), NamedQueries with JoinFetch, Enum as attribute.
  • Page 03: Generic DAO, application transaction methods, generic methods to populate query parameters.
  • Page 04: Façades an transactions, using the method findReferenceOnly, care using the entityManager.merge() method.
  • Page 05: Filters.
  • Page 06: ManagedBeans. How to inject a ManagedBean inside another ManagedBean, observations about @ViewScoped.
  • Page 07: JSFMessageUtil.
  • Page 08: Configurations file: log4j.properties, messages.properties.
  • Page 09: xhtml pages, Facelets.
  • Page 10: Primefaces AutoComplete, JSF Converter with “forClass”.
  • Page 11: Easiest way to use CSS/javascript/images with JSF.
  • Page 12: “web.xml” configurations.
  • Page 13: Increasing the security of your application.
  • Page 14: Running the application.

The application that you will find in here will have a Dog and Person CRUD (Create, Read, Update, and Delete); only the ADMIN will have access to the Dog CRUD. Before you run this application you should create a database named “JSFCrudDB”.

Quatro soluções para LazyInitializationException

Olá, tudo bem?

No post de hoje veremos como tratar o famoso erro LazyInitializationException. Vamos ver 4 modos de evitar que esse erro aconteça, a vantagem e a desvantagem de cada abordagem e ao final, veremos comentários sobre o modo de como o EclipseLink trata essa situação.

Para simular e tratar o LazyInitializationException iremos utilizar um projeto EJB 3 com JSF 2.

O que veremos no post de hoje:

  • Página 3: Entenda o problema, por que o erro LazyInitializationException acontece?
  • Página 4: Carregar coleção por anotação
  • Página 5: Carregar coleção por Open Session in View (Transaction View)
  • Página 6: Carregar coleção por Stateful EJB com PersistenceContextType.EXTENDED
  • Página 7: Carregar coleção por Join Query
  • Página 8: Considerações sobre EclipseLink

Ao final do post você irá encontrar o código para download.

Atenção: Para esse post foi utilizado um código bem simples, e que não utiliza padrões de projeto. O foco desse post é destacar as soluções para o LazyInitializationException.

Apesar do exemplo que é utilizado nesse post ter sido criado com JSF + EJB, as soluções aqui se aplicam para o tratamento em qualquer ferramenta web. Tanto JSF, JSP com Servlets, JSP com Struts, JSP com VRaptor, etc.

O único exemplo que não é aplicável para a tecnologia JSE (em geral definido como Desktop) é a solução do EJB.

Four solutions to the LazyInitializationException

Hello, how are you?

In the post today we will talk about the common LazyInitializationException error. We will see four ways to avoid this error, the advantage and disadvantage of each approach and in the end of this post, we will talk about how the EclipseLink handles this exception.

To see the LazyInitializationException error and to handle it, we will use an application JSF 2 with EJB 3.

Topics of the post:

  • Page 3: Understanding the problem, Why does LazyInitializationException happen?
  • Page 4: Load collection by annotation
  • Page 5: Load collection by Open Session in View (Transaction in View)
  • Page 6: Load collection by Stateful EJB with PersistenceContextType.EXTENDED
  • Page 7: Load collection by Join Query
  • Page 8: EclipseLink and lazy collection initialization

At the end of this post you will find the source code to download.

Attention: In this post we will find an easy code to read that does not apply design patterns. This post focus is to show solutions to the LazyInitializationException.

The solutions that you will find here works for web technology like JSP with Struts, JSP with VRaptor, JSP with Servlet, JSF with anything else.

The only page code that does not apply to JSE (usually Desktop applications) is the Page 6 with EJB.