Candle at the Pool

Flattr4j: A Java library for Flattr

The microdonation service Flattr is currently working on a REST API for accessing the flattr account. I have created a Java client called flattr4j, which makes accessing the API easier.

The client also supports the Open API and JavaScript API via a JSP taglib, as well as Maven and Spring. As long as the Flattr REST API is not public yet, you need to send a proposal to Flattr.

Have fun trying it out! Your feedback is welcome.

How to fetch a random entry with Hibernate

I recently found myself in the situation where I needed Hibernate to query a single, random entry from a table of Picture entities.

There is a simple way. Some DBMS allow to shuffle the result set by bringing the rows into a random order. For instance, in MySQL it is possible to use a query like this:

SELECT id FROM picture ORDER BY rand() LIMIT 1;

Since Hibernate delegates unknown function calls to the underlying DBMS, rand() could be used in a HQL query as well:

q.createQuery("FROM picture ORDER BY rand()").setMaxResults(1);

However this query would require MySQL, so we would sacrifice the benefit of Hibernate acting as an abstraction layer to the underlying database. HQL on the other hand does not offer a similar function.

A solution is to use the pagination technique. First we count the number of entries, and then select a random entry using setFirstResult(). With Hibernate Criteria, it would look something like this:

Criterion restriction = yourRestrictions;
Object result = null;  // will later contain a random entity
Criteria crit = session.createCriteria(Picture.class);
crit.add(restriction);
crit.setProjection(Projections.rowCount());
int count = ((Number) crit.uniqueResult()).intValue();
if (0 != count) {
  int index = new Random().nextInt(count);
  crit = session.createCriteria(Picture.class);
  crit.add(restriction);
  result = crit.setFirstResult(index).setMaxResults(1).uniqueResult();
}

restriction contains further restrictions to the result set (like only pictures that have been published). At the end, result contains a random single entry from the Picture entity, or null if the result set was empty.

The Silence of the Fans

Hmmm... For a manufacturer who makes a big deal out of the quality of his power supplies, this is a rather sad picture:

All capacitors on the photo are bulged or have leaked. I had two power supplies of this brand in the upper price range. Both failed shortly after the warranty ended. At least it was a well-invested lesson.