Hibernate integration testing strategies
Introduction I like integration testing. As I explained in this article, it’s a good way to check what SQL queries are generated by Hibernate behind the scenes. But integration tests require a running database server, and this is the first choice you have to make. Using a production-like local database server for Integration Testing For a production environment, I always prefer using incremental DDL scripts, since I can always know what version is deployed on a given server, and which scripts required to be deployed. I’ve been relying on Flyway to manage… Read More
How to implement Equals and HashCode for JPA entities
Introduction Every Java object inherits the equals and hashCode methods, yet they are useful only for Value objects, being of no use for stateless behavior-oriented objects. While comparing references using the “==” operator is straightforward, for object equality things are a little bit more complicated.
How to fetch entities multiple levels deep with Hibernate
Introduction It’s quite common to retrieve a root entity along with its child associations on multiple levels. In our example, we need to load a Forest with its Trees and Branches and Leaves, and we will try to see have Hibernate behaves for three collection types: Sets, Indexed Lists, and Bags.
A beginner’s guide to Hibernate fetching strategies
Introduction When it comes to working with an ORM tool, everybody acknowledges the importance of database design and Entity-to-Table mapping. These aspects get a lot of attention, while things like fetching strategy might be simply put-off. In my opinion, the entity fetching strategy shouldn’t ever be separated from the entity mapping design, since it might affect the overall application performance unless properly designed. Before Hibernate and JPA got so popular, there was a great deal of effort put into designing each query, because you had to explicitly select all the joins you… Read More
A beginner’s guide to Hibernate flush operation order
Introduction As explained in this article, Hibernate shifts the developer mindset from SQL to entity state transitions. A JPA entity may be in one of the following states: New/Transient: the entity is not associated with a persistence context, be it a newly created object the database doesn’t know anything about. Persistent: the entity is associated with a persistence context (residing in the 1st Level Cache) and there is a database row representing this entity. Detached: the entity was previously associated with a persistence context, but the persistence context was closed, or the… Read More
How do Set and List collections behave with JPA and Hibernate
Introduction Hibernate is a great ORM tool, and it eases development considerably, but it has a lot of gotchas you must be aware of if you want to use it properly. On medium to large projects, it’s very common to have bidirectional parent-child associations, which allow us to navigate both ends of a given relationship. When it comes to controlling the persist/merge part of the association, there are two options available. One would be to have the @OneToMany end in charge of synchronizing the collection changes, but this is an inefficient approach…. Read More
Book Review – How to win friends and influence people
Soft skills When working as a Scrum Master you have to constantly make sure the Scrum principles are followed, but there are also other aspects that requiring handling as well, and they are more subtle yet equally important for successfully managing developing teams. I am talking about soft skills such as managing team conflicts, encouraging people, knowing how to challenge your team members. There are times when some of your guys behave inappropriately versus others, or their attitude/behavior is counter-productive and you have to step in and correct these problems with diplomacy… Read More
Lock processing logic by customer
Introduction In the current application we are developing there was one use case where we wanted to synchronize message processing by message provider (customer generating those messaging). The flow looks something like this: So messages may come randomly since there are more customer jobs running in parallel, but we want to ensure that messages belonging to the same customer are processed one after the other (analog to the Serializable database isolation level) while allowing messages coming from different customers to be processed in parallel.
Why I like Spring @Autowired for List types
Spring Framework dependency injection is great, and almost every Java developer uses it nowadays. Using @Autowired to inject Java Beans is trivial, but we can also use this annotation for java.util.List, or java.util.Map as well. The former will inject a list of all Java Beans matching the List’s Generic type, while the latter will create a map of these beans mapped by their names. How I’ve been taking advantage of this feature? Since I was developing an application which has a framework module and a specific customer implementation module, there were cases… Read More