JPA and Apache Derby

I have a Derby database, and I’m looking to query and update it from a java application. I’m using JPA to handle this. My persistence.xml file is as follows:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="test">

        <class>dan.TestEntity</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:/home/dan/Derby/db1;create=true" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
            <property name="hibernate.show_sql" value="true" />
        </properties>

    </persistence-unit>

</persistence>

In order to query a single entity from the database, I use the find method provided by EntityManager:

TestEntity entity = entityManager.find(TestEntity.class, id);

When updating records in the database, we use the merge method provided by EntityManager:

entityManager.merge(testEntity);

When creating a new record, we use the persist method:

entityManager.persist(testEntity);

Updating hot deployment scan period in JBoss 5

The default scan period was set to 60 seconds.  During development, I wanted a shorter waiting period before my new version of a .war file was deployed.  This can be changed by editing hdscanner-jboss-beans.xml within the deploy folder.

The HDScanner bean contains a property “scanPeriod”, which specifies the interval at which JBoss scans for new deployments.

Eclipse Databinding Decoration

On a wizard I’ve developed, I’ve made use of the Eclipse databinding framework to bind UI widgets to the model behind the wizard.  This framework generally works pretty well, but I was having issues when I wanted to hide the decoration of a control based on the selection of other controls in the wizard.

So here’s the situation:  I have a text control, and I have created a validator on the control which returns an error status if the text control is empty.  I have set the default decoration on the text control. 

I also have a checkbox on the wizard.  If it isn’t checked, I set the text control to be disabled.  If the control is disabled, I want its decoration to be hidden.

It turns out this is harder than expected.  In the end, the strategy I adopted was as follows:

I created my own control decoration updater:

class CustomControlDecorationUpdater extends ControlDecorationUpdater {
  private ControlDecoration controlDecoration;
  private IStatus lastStatus;
  private boolean showValidation = false;

  @Override
  protected void update(ControlDecoration decoration, IStatus status) {
    if (this.controlDecoration == null) {
      controlDecoration = decoration;
    }
    this.lastStatus = status;
    super.update(decoration, status);
    if (!showValidation) {
      controlDecoration.hide();
    }
  }

  public void setShowValidation(boolean show) {
    this.showValidation = show;
    if (controlDecoration == null || lastStatus == null) {
      return;
    }
    update(controlDecoration, lastStatus);
  }
}

This control decoration updater allows me to tell it whether to hide the decoration. Now I add a listener to my checkbox which controls whether the text control is enabled.

button.addSelectionListener(new SelectionAdapter() {

  @Override
  public void widgetSelected(SelectionEvent e) {
    customControlDecorationUpdater.setShowValidation(button.getSelection());
  }
});

Reverting change using Gerrit

The development environment we have on my current project is pretty good – we’re using the Jenkins (Open source Hudson) continuous integration system, and we’re using the Git version control system.  This is a distributed version control system, so we push commits to a remote repository once we’re happy with them.  Using a distributed system like Git allows us to use Gerrit to mandate that commits are code reviewed before they are submitted to the remote repository.

So the system allows to spot most mistakes early, Jenkins ensures that the code builds and all the tests pass.  Forcing a code review means that you get at least one other pair of eyes on your code before it is submitted.

If, despite all these checks, something makes it onto the remote master branch which causes problems at run time, what are your options to fix things?

A strategy I have used is to rebase my local version of master so that it no longer contains the offending commit, and then force push to the remote machine, so that the remote repository no longer contains it.  However, if other users have already pulled from master, then their local commit histories will already contain the offending change.  When they push, the offending change will also be pushed to Gerrit along with their changes.

Fortunately, Gerrit comes to your rescue with the “Revert Change” button which can be applied to changes which have already been submitted.  This adds a new commit to Gerrit which undoes the effect of the change which you are looking to revert.

Using JPA within Eclipse RCP

OK, so I have an RCP application which is using JPA to persist some data model objects to a database. I am using the Hibernate implementation of JPA, and I am using a H2 database.

In order to bundle up my dependencies on Hibernate and H2 in a nice RCP way, I have put all the Hibernate libraries into a plug-in called org.hibernate, and all my H2 libraries into a plug-in called org.h2. I can then list these as dependencies within the manifest of my plug-in projects.

Some of the JPA tutorials I looked at when getting started were:
http://docs.jboss.org/hibernate/core/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html
http://www.vogella.de/articles/JavaPersistenceAPI/article.html

When I had created my JPA persistence classes, I thought I’d write some unit tests to ensure that they were working as expected. Everything compiles, but when I run my unit tests as JUnit Plug-in tests, I get the following error:

javax.persistence.PersistenceException: No Persistence provider for EntityManager

Uh-oh, this looks like some kind of class loading issue, right? After a search online, I came across the suggestion that the Eclipse buddy class loading mechanism might be able to help.A useful article on it can be found here:

http://wiki.eclipse.org/index.php/Context_Class_Loader_Enhancements

In my org.hibernate plug-in, I updated the manifest to include the line “Eclipse-BuddyPolicy: registered”. This means that, when at runtime the org.hibernate bundle is looking for the persistence unit declared in your persistence.xml file, it will consult all dependent bundles that explicitly register themselves as buddies to it. In the manifest of my client plug-in, I added the line “Eclipse-RegisterBuddy: org.hibernate”, in order to register it as a buddy.

Now Hibernate is able to find my persistence unit, and all of the classes defined within it, at runtime, and everything runs fine.