Accessing Database in Java with JDO

I haven’t written Java code to access relational database for a while. Over the years there have been lots of progresses, especially with various Object Relational Mapping (ORM) frameworks. With these frameworks, accessing relational databases becomes pretty easy. In some cases, you don’t have to know SQL at all. Of course understanding SQL is always an advantage.

In my recent project, I have to use relational database as object repository. Like other existing projects, the build process has been nicely automated and I don’t even have to know about the framework used, which is good when it’s under pressure to deliver something but not so good to understand a technology. To my curiosity, I decided to try a new project with database interaction from scratch.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

One leading ORM framework is the Java Data Objects (JDO) specification started by Sun Microsystem in 2000. It’s now an Apache project. The JDO provides a consistent interface across different data sources, including relational databases, XML, LDAP, etc. An introduction can be found at here. Another competing spec from Sun/Oracle is the Java Persistence API (JPA). There is a good doc comparing these two. Note that it’s from JDO camp and could be biased.

To get me started, I tried to find an easy HelloWorld type of sample and found one from It has a nice high level introduction on, but has not provided ready to run code, especially these build scripts or POM. Luckily, I found a few samples on Github by Andy Jefferson and others. The full URL to the GIT repository is: You can just clone the repository to your own IDE. In my case, I use Netbeans and opened one of the projects “DataNucleus AccessPlatform Tutorial for JDO”. There are other samples for more advanced use cases in the repository.

Building the project and running the are pretty straight-forward. It uses h2 in memory database out of box. To switch to other database, for example, MySQL, you need to tweak it a little bit.

First, create a database in MySQL. I just named it as doublecloud using SQL as follows. If you use MySQL Workbench, it’s even easier. Just look for other schemas in the left side pane and right click for “Create Schema …” context menu which pops a wizard to guide you through.

CREATE SCHEMA `doublecloud`;

Second, you need to un-comment the MySQL portion in the META-INF/persistence.xml file and comment out the h2 related portion. You should change the database server name, database name, user name, and password to reflect your environment.

           <!-- ENABLE THESE FOR MySQL/MariaDB -->
            <property name="javax.jdo.option.ConnectionURL" value="jdbc:mysql://"/>
            <property name="javax.jdo.option.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
            <property name="javax.jdo.option.ConnectionUserName" value="root"/>
            <property name="javax.jdo.option.ConnectionPassword" value="secret"/>
            <property name="javax.jdo.option.Mapping" value="mysql"/>

Third, change the pom.xml and un-comment the MySQL driver and datanucleus RDBMS adapter. You can leave the h2 dependency there.

        <!-- ENABLE THIS FOR MYSQL -->
            <version>[4.0.0-m1, )</version>

After these changes, you can build the code and run the again. Then you should see 4 tables created in the doublecloud database: BOOKS, INVENTORIES, INVENTORY_PRODUCTS, PRODUCTS for the 3 Java objects, Book, Inventory, and Product. Book is a subclass of Product. The inventory is a simple container that holds a name as primary key, and a Set of Product objects.

Once the sample runs, it should serve a good starting point to develop your persistence code. You can add new, remove existing, Java classes, change the persistence file so that the framework knows what classes to enhance.

Using a framework like datanecleus really makes life easier. There is of course a performance penalty. I haven’t compare this with using JDBC directly, but I am sure JDBC will be faster with a prices that you write a lot more code and know enough SQL. It’s really a trade-off. In general, it should be good to start with a ORM framework and then optimize it when performance becomes a problem.

This entry was posted in Software Development and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.