30 Mai 2008

Problem with enum in JPQL solved!

Mike Keith solved my enum problem in JPQL.

I forgot to fully qualify the enum. So if the query is correct it works:

String queryString = "SELECT a FROM Address a " +
"where a.addressType = jpa.model.AddressType.OFFICE";

Thanks a lot Mike!

New features in JPA 2.0

Today I had a look at the early draft of the JPA 2.0 spec and like. Here are my highlights of the new features:
  • Added support for persistently ordered lists using OrderColumn and provider-managed ordering column.
    -> Greate! That was really missing!
  • Defined support for foreign key mapping strategy for unidirectional one-to-many relationships.
    -> Very good. It's not very handy to use a relationship table in a one-to-many mapping
  • Added clear method to EntityManager interface to allow entities to be evicted from the persistence context; added CLEAR cascade option.
    -> Bulk Update and Delete will be even more useful
  • Added Cache interface.
    -> I'm not sure if it's a great idea, but who knows...
  • Added support for pessimistic locking and new lock mode types.
    -> I hope JPA will also provide support for SELECT FOR UPDATE
  • Added overloaded find and refresh methods added to support locking with standardized and vendor-specific properties and hints.
    -> Very good, it's no longer required to define an extra query
  • Added standardized hint javax.persistence.lock.timeout for use in locking configuration.
    -> Great idea!
  • Added the standardized properties javax.persistence.jdbc.driver, javax.persistence.jdbc.url, javax.persistence.jdbc.user, javax.persistence.jdbc.password for use in persistence unit and entity manager factory configuration.
    -> Super! It will be simpler to change the persistence provider in standalone environment. And it will be easier for the tools provider to support persitence.xml properties completion.
  • Added Query getNamedParameters and getPositionalParameters methods.
    -> good thing, at least for debugging
I hope there will be more of them in the next version of the spec...

Missing features in JPA

During the past JPA course at the university of applied sciences in Bern I discovered two missing features in JPA:

1. static inner classes in constructor expressions


// Constructor Expression
String queryString =
"SELECT NEW Outer.Inner(e.name) FROM Employee e";

Query query = em.createQuery(queryString);
List outer = query.getResultList();
for (Outer.Inner inner : outer) {

Where Outer.Inner is a static inner class in Outer:

public class Outer {
public static class Inner {
private String name;
public Inner(String name) {
this.name = name;
public String getName() {
return this.name;

In my opinion this should work. Because I can instanceiate Inner like this:
Outer.Inner in = new

2. Enumerations in JPQL

For example, this works fine:

String queryString =
"SELECT a FROM Address a where a.addressType = :type";

Query query = em.createQuery(queryString);
query.setParameter("type", AddressType.OFFICE);

But this does not:

String queryString = "SELECT a FROM Address a where a.addressType = AddressType.OFFICE";
Query query = em.createQuery(queryString);

Surprising this works in Hibernate but not with TopLink:

String queryString = "SELECT a FROM Address a where a.addressType = 'OFFICE'";
Query query = em.createQuery(queryString);

I hope this two features will be contained in the next JPA relase!

29 Mai 2008

Only two reasons for declaring an interface

I had several discussions about when it makes sense to declare an interface in Java.

I only found two possible reasons!
  1. If you could have more than one implementation of this interface (I think this is the original intention for interfaces)

  2. If you want to use proxies. (i.e. remoting or dynamic proxies)
In any other cases you should think twice if there is really a need for an interface!
This sounds very simple in a way, but I think this is an important finding.

If you know any other important reason - please let me know!

12 Mai 2008

Even more lecturing

I will start two new courses at the University of Applied Sciences of Bern this Fall.

One will be a database basic course in the Master of Advanced Studies (MAS) in Medical Technology class and the other is an advanced database concepts course for a MAS in Information Technology class.

It will be some hard work to prepare but I'm sure to learn a lot of databases techniques for my daily work!