Persisting Jython Objects Using DB4O

If you have been following my blog, you will know that I have been playing with the following technologies: DB4O (an open source object database), Jython (Python 2.5, implemented on the JVM), and Django (A python based MVC-style web application framework in the same class as Ruby on Rails).

So far, in tutorials I wrote in November of last year, we have seen that Jython allows us to write Python code that uses Java libraries, such as DB40. I altered the standard DB4O Formula One tutorial using Jython instead of Java, and showed that we can persist and retrieve objects using this technology.

I also mentioned that Django, a Python based web application framework, also runs on Jython.

Which leads us to this entry, where I will start thinking and tinkering to find a way to use DB4O with Django/Jython rather than a relational database.

I am doing this for fun, not for profit. I am not an expert with any of the technologies I am using. I stand in awe of the very talented open source programmers who precede me. I hope to learn as much as I hope to teach and share what I know, or think I know.

Persisting Objects:

In my previous example, I created Plain Old Java Objects (Pojos), and created a JAR. I used the classes in the JAR to create the objects I persisted using DB4O. I have since decided that this is not desirable. I think this creates a barrier to Python programmers who want to use Django, Python and DB4O togther.

Therefore, I have resolved to persist Python classes by providing the means to automagically inspect these classes and generate the JAR files. I shared these thoughts over a month ago.

Before writing any code, I just want to reflect on the task at hand. Let’s gather our assumptions.

  • For every Python data type, there is a corresponding Java data type.
  • A “simple” object is based on a class that contains primitive types only.
  • A complex object contains other objects, or collections of objects.
  • We may need to make two passes to persist objects – first simple objects, and then complex.
  • A class contains an implementation as well as data, and we want to persist both.
  • If we change the definition of a class in our project, we need to think about what happens to our previously stored objects.
  • Our Jython classes must inherit from a single class only, or we will not be able to produce an equivalent Java class.

In Django, we define models, and then use a command (python manage.py syncdb) to generate the corresponding database. The database is created automagically, based on the settings in the settings file (settings.py). The model is able to perform basic operations to create, read, update and delete objects (CRUD).

We want our DB4O objects to behave in the same way – as much as possible, we do not want to violate the expectations of Django programmers who have used Django with relational databases.

In my next entry, I will try to write code that creates a Java class based on a Jython class and saves it in a file. I will keep it simple to start, and the code will evolve as we go.

Our first model will be simple – a class with a few attributes of various types. The output will be a class files – we will not worry about creating the JAR just yet.

Once we have produced a simple class file, we will produce class that make two passes and resolve inheritance issues as well as contained objects that inherit from previously defined classes.

Code to come in a day or two.

Learn Python, Why Don’t You?

Since I have been writing about Jython, a version of Python implemented on the JVM, I thought I would provide a link to some Python resources for Java programmers who are using DB4O: Learn Python, why don’t you?

If you like what Java has to offer in terms of class libraries, but you like the Python language and its idioms, then Jython is the best of both worlds.

To prepare for my next installments on Jython, Django and DB4O, read about introspection in Python.

Good Advice For Any Developer

I am working to use reflection to generate a POJO from data models defined in Jython – DB4O can only persist Java ojects. However, as I sort that out and  prepare for my next DB4O/Jython/Django post, let me share this good advice for any developer who builds any sort of web application in any language.

These seven habits for developing secure applications target PHP developers, but they constitute good advice for you, even if you are working in another language. Take heed.

« Previous Entries Next Entries »