Friday, May 23, 2008

Entity Method Instances From hibernate.cfg.xml

Wouldn't it be cool if you could obtain an instance of a particular annotated method from a Hibernate entity by simply examining the hibernate.cfg.xml file? It's not as difficult as you may think.
The key to the whole process is knowing how to read the configuration file and manipulate what it returns. Let's examine some code that does just that. We'll examine all the classes in our config file and retrieve information on each of the methods annotated with @Id. We'll put this information in a map with the map's keys being the classes themselves and the values being Hibernate IdMetaData objects.

Map idMap = new HashMap();
1 Configuration config = new AnnotationConfiguration().configure();

2 for (Iterator iter = config.getClassMappings(); iter.hasNext();)
3 RootClass root = ((RootClass);

try {
4 Class clazz = root.getMappedClass();

5 Method idGetter = root.getIdentifierProperty()
6 Method idSetter = root.getIdentifierProperty()
7 Class type = idGetter.getReturnType();
8 IdMetaData md = new IdMetaData();
9 md.setIdGetter(idGetter);
10 md.setIdSetter(idSetter);
11 md.setType(type);
12 idMap.put(clazz, md);
13"mapping Id for " + clazz.getName());
catch (Exception e) {
throw new Error("unable to initialize annotation map");
Line 1 is the standard way to generate a Hibernate configuration instance. The Iterator of line 2 returns a Hibernate RootClass (line 3) for each of the defined classes in the config file. This class is the gateway to a lot of information.
root.getIdentifierProperty() in lines 5 and 6 returns a Hibernate Property object of the method marked with the @Id annotation. This allows access to the actual getter and setter methods. IdMetaData is a simple bean class that holds any information obtained from the RootClass object. Line 12 fills a HashMap with the class as the map key and the IdMetaData as the value. Once you know the trick you can programatically get at any information you wish to.
Developers read APIs, Engineers read the source code!

Wednesday, May 21, 2008

Whence the name?

The dining philosophers problem is an illustrative example of a common computing problem in concurrency.
Five philosophers sit at a table doing one of two things - eating or thinking. While eating, they are not thinking, and while thinking, they are not eating. The five philosophers sit at a circular table with a large bowl of spaghetti in the center. A fork is placed in between each philosopher, and as such, each philosopher has one fork to his or her left and one fork to his or her right. As spaghetti is difficult to serve and eat with a single fork, it is assumed that a philosopher must eat with two forks. The philosopher can only use the fork on his or her immediate left or right.

The philosophers never speak to each other, which creates a dangerous possibility of deadlock when every philosopher holds a left fork and waits perpetually for a right fork (or vice versa).

Visit the DP Problem link on the right to see how it turns out.