Posts

Showing posts with the label Hibernate

Spring Data JPA and H2

Image
Spring Data Spring Data makes persistence much easier. I personally not a huge fan of JPA and ORMs.  However, I know lots of engineers like it and I respect different preferences. Have different preferences in different services is totally fine. However, doing that in shared libs is NO-NO. Today I want to cover a bit more about how we can use Spring Data JPA and H2 database for testing. Speed matters. Combining H2 and MockMVC we can speed up tests considerably. I made a simple POC showing how we can configure and use Spring Data JPA with H2 and do proper testing using MockMVC capabilities. I also show how to use the H2 Console so we can query the database. Spring-Data makes persistence much more concise and leverages lots of common functionality for us. So Let's get started. 

Hibernate Reactive with Spring Boot 2

Image
Reactive programming is not a new thing. Thanks to reactivex and rx-java it's a reality for almost 10 years. There are other implementations like Project Reactor  and even Vert.x . However, for Databases, we are still not there. Some databases have better support than others like Cassandra and Redis have decent support in drivers but JDBC and JPA are not quite there yet. Hibernate just went reactive. There is a hibernate-reactive project now(still beta 1.0) but looking promising. So today I want to share some simple POC I made to make Hibernate Reactive work with Spring Boot 2.x using webflux: using Netty of course. So I made a video going throught the code and explaining how it works. I personally worked with all sorts of persistence on the last +17 years on technology but not an ORM fan nowadays. However I always appreciate performance and better APIs, so I think this is promising even if is for legacy application modernization.

DAO Generico

O Design Pattern DAO é um padrão para se encapsular o acesso a banco de um determinado Objeto. Existem muitas criticas sobre esse padrão, dentre elas a que você prolifera muitas classes do estereótipo DAO pelo seu projeto e ma verdade elas só tem um conjuntos de métodos find*. Eu concordo com isso, achei um artigo no Developer Works da IBM, que na minha modesta opinião é uma ótima solução. Você cria uma DAO Genérico e depois só precisa criar interfaces e named querys no Hibernate. A solução envolve Hibernate e Spring + AOP. É Muito bem bolada. Recomendo a leitura do artigo, no final do artigo tem um link para os fontes. Vou resumir o que a solução faz: Existe uma interface GenericDao que define o comportamento de um DAO, temos uma implementação padrão para essa interface que é a classe GenericDaoHibernateImpl essa classe implementa mais três interfaces: FinderExecuter que é o que o interceptor do Spring ira interceptar. E a outra é FinderArgumentTypeFactory Que define uma factory...