Identifying tables used by Java entities

While going through the logs of a production system, the system administrator observed some strange InvalidStateException errors coming from hibernate. The exceptions looked something like:

org.hibernate.validator.InvalidStateException: 
     validation failed for: com.example.model.Concept
     ...

One of the assumptions was that these errors were due to some database problems. To check the assumption, he needed to know which tables were involved in the errors. The problem was that the logs only gave him the names of the entity classes (e.g., com.example.model.Concept) without the names of the tables involved in the mapping. Thus, one thing we learnt is that we needed to extend the logging infrastructure to export the table names as well. However, this still did not solve the existing problem because the system was already in production and could not be changed easily.

To dig into the problem, he would have needed to open Eclipse, search for the involved classes and identify the associated tables. The Java code associated with the error looked like a regular hibernate annotation:

@Entity
@Table(name="CONCEPT")
public class Concept {...}

In our example, com.example.model.Concept is associated with the CONCEPT table.

However, given that he did not have access to such an environment, he was stuck. He wished of having a simple file with the mapping of all classes. It turned out that the problem is straightforward with Moose. The below script retrieves the mapping and puts it in a simple tab separated file that can be viewed with Excel:

model allClasses do: [ :each |
   (each isAnnotatedWith: #Table) ifTrue: [
   Transcript
      show: each mooseName;
      tab;
      show: ((each annotationInstances detect: [:ann | ann name = #Table])
             attributes detect: [:attr | attr name = #name]) value;
      cr ] ]

The whole cycle, from problem identification to obtaining the mapping, took somewhere around 15 minutes. After this small investment, the original problem went from monumentally difficult to doable.

Posted by Tudor Girba at 20 July 2013, 10:12 pm with tags assessment, story, moose, tooling, spike link
|