When you use Hibernate for ORM and come across some functionality that requires multi threading, there are many pitfalls that might make life difficult for you. This blog will focus on those problems. Conclusion is: don't use hibernate managed objects in multiple threads.
(more...)
Tags: lazy initialization exception, lost update, multi threading, transaction
Filed under Hibernate | 7 Comments »
I've just read Robert C. Martin's Clean Code and Kent Beck's Implementation Patterns back to back. I actually picked up Clean Code first because my colleagues were raving about it. But then Robert Martin's book quotes from Kent Beck's book on the third page of the first chapter already, and disagrees with the quote, so I decided it'd be fun to read Implementation Patterns too. ![]()
(more...)
Filed under Architecture, Hibernate, Java, Testing | 11 Comments »
Two years ago I blogged about annotations and that I considered them to be A Bad Thing. It seems I will have to eat my words. I am actually using them to the hilt in my current project.
We use JPA and specific Hibernate annotations on our entities. See for example these annotations on a field:
@OneToMany(mappedBy = "changePlan", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@org.hibernate.annotations.Sort(type = org.hibernate.annotations.SortType.COMPARATOR,
comparator = PositionableComparator.class)
private SortedSet
(more...)
Filed under Hibernate, Java, Spring | 4 Comments »
Lately I ran into an annoying problem with Hibernate. I tried to do pagination on a query result which was doing an SQL-JOIN under the hood. The query before paging returned about 100 results. When I turned on paging (with 20 results per page) all the pages had less than 20 results!
The reason for this is that with a JOIN there can be duplicate results and those results are filtered out after pagination is done. In this blog I will explain how to solve those problems and it also a cleaner way to build your Criteria queries.
(more...)
Tags: criteria, pagination
Filed under Hibernate, Java | 9 Comments »
This second part on Spicefactory will delve into the details of writing a Grails plug-in for this framework. Be prepared to read a lot of code!
Tags: AS3 EntityManager, grails-flex-webtier plug-in, grails-spicefactory plug-in, JPA EntityManager, pimento, spicefactory
Filed under Flex, Grails, Hibernate | No Comments »
Today I spend some hours trying to fix a hibernate bug in our application. I changed the configuration just a little and it seemed that Hibernate was unable to handle this. I'd even found a bug report in Hibernate JIRA that described the same situation. I was on the brink of downloading the sources and trying to fix the problem in Hibernate... turns out there was an error in our configuration! This is to say, the model we wanted to configure could be configured in a non straight forward way. Apparently from the JIRA issue there are more people that find it difficult to come up with the correct configuration for this situation. Let me try to help them with a little example.
Spring is a great framework for dependency injection and it comes with a lot of support classes and utilities for all kind of things. Hibernate is a persistence service with a lot of useful features, that is relatively easy to use. Configuring both frameworks is not always easy. Configuring them together is sometimes hard and it is easy to make mistakes.
This blog addresses a problem in a configuration that is fairly common: use Spring for transaction management on top of a JTA provider and use Hibernate for persistence. Transaction demarcation is easy and declarative with Spring. The problem is that Hibernate sometimes needs to detect the current transaction and this needs to be configured. This leads to hard to detect bugs in applications that rely on auto flushing.
(more...)
Tags: auto flush, Hibernate, JTA, Spring
Filed under Hibernate, JTA, Spring | 4 Comments »
One of the challenges we are facing in our project is connecting antique display devices to the brand new travel information system we are building. If you have traveled by train in the Netherlands you are familiar with them: large displays with booklets for destinations and departure times. It contains a number of booklets which are controlled by a stepper engine. The devices are called CTA's, were developed in the eighties and are a solid piece of engineering. Behind the track indicator, which doubles as a door, is a temperature control for the heating, a power socket for connecting electrical equipment, a telephone socket and a connector for testing. Oh yes, did I mention it is heavy?
Filed under Agile, Hibernate, Javascript, Project Management, Testing | 2 Comments »
A thing I was playing with today was many-to-many relationships in Grails to create a Tag Cloud. To create a Tag Cloud, I must have a set of key/value pairs, each with a label and a value of the label, which could look like this:
['Java': 5, 'Grails': 16, 'Groovy': 12]
But to query this, I need to query a many to many relationship and produce the above result. This blog will describe how to do this with HQL, Criteria and the HibernateCriteriaBuilder.
(more...)
Filed under Grails, Groovy, Hibernate, Java | 6 Comments »
Most programmers come into contact with Hibernate proxies when they face the ominous LazyInitializationException. The bad taste is hard to wash away, but Proxies are a necessary “evil” when working with Hibernate. This article will dig into why proxies are important. It will also point out some pitfalls that occur often when working with proxies.
(more...)
Filed under Hibernate | 14 Comments »