Sunday, 27 July 2014

From Hibernate to EclipseLink

I've decided to try porting my little application from Hibernate to EclipseLink.

I am not currently using a lot of Hibernate specific functionality. The ones that occur to me at the moment is:
  • Hibernate Filters
  • Hibernate DELETE_ORPHAN


Filters are, as far as I know, currently not a part of the JPA Specification. Every ORM has its own implementation of dealing with it.

Below are the two different (very different!) implementations for Hibernate and EclipseLink. I find the Hibernate one to be more powerful.


Definition of the filter:
@FilterDef(name = "activePersons", defaultCondition="active = 1")
package mmud.database.entities;
Using the Filter on Entities:
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.Filters;

@Filters({ @Filter(name = "activePersons") })
public class Person implements Serializable { ...
public class Room implements Serializable {

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "room")
   @Filter(name = "activePersons")
   private Set<Person> persons = new HashSet<>();
Enabling the Filter, upon starting a session:
// Hibernate specific
Session session = 
    ((org.hibernate.ejb.EntityManagerImpl)em.getDelegate()).getSession();// JPA1.0
// Session session = getEntityManager().unwrap(Session.class); // JPA2.0


EclipseLink uses the AdditionalCriteria2 3 annotation on Entity level.
@AdditionalCriteria("(:activePersonFilter <> 1 or = 1)")
public class Person implements Serializable { ...
getEntityManager().setProperty("activePersonFilter", 1); // turns filter on

Unfortunately, I cannot not set it, as that will trigger the following:
So I am obligated to turn the filter on or off at the boundary where the entityManager (or EntityManagerFactory) is first called.

And there is not a convenient way of turning a filter on or off. Right now, the turning on/off is part of the subquery.4

However, I do like the fact that my AdditionalCriteria suddenly works everywhere once I turn it on. I do not have to set specifics on the fields of an Entity. Of course, this does limit the flexibility, but in my case it is not an issue.

Deleting Orphans

Well, Hibernate was one of the first to implement the Delete-orphan functionality and it took a while for it to become meanstream in the JPA specification. But it's there now, and should be supported by all ORMs.


@OneToMany(cascade = CascadeType.ALL, mappedBy = "belongsto")
@Cascade({ org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private Set<item> items;

JPA 2.0

@OneToMany(cascade = CascadeType.ALL, mappedBy = "belongsto", orphanRemoval = true)
private Set<item> items;



Apparently JPA doesn't have an sha1 function, yet mysql does. Hibernate had no problems with it, so I looked for a solution from EclipseLink5.
@NamedQuery(name = "User.authorise", query = "select p from Person p WHERE = :name and p.password = sha1(:password)")

JPA 2.1

It turns out JPA5 has a specific reserved word called "FUNCTION" to support this, that I quite like. It prevents me from having to write a specific MySQL Dialect.

Of course, it does tie me to the MySQL implementation, but you cannot have everything.
@NamedQuery(name = "User.authorise", query = "select p from Person p WHERE = :name and p.password = FUNCTION('sha1', :password)")


I found EclipseLink to be very strict with the interpretation of JPQL.

Issue 1 - References

For example, it complains about:
SELECT max(id) FROM ItemDefinition i
Wanting something more along the lines of:
SELECT max( FROM ItemDefinition i

Issue 2 - Like statements

Then there's an issue with the following query statement:
SELECT s FROM SillyName s WHERE :name like
That gave me the error message "You have attempted to set a value of type class java.lang.String for parameter name with expected type of class java.lang.Boolean". I had to do some voodoo magic to make it work:
SELECT s FROM SillyName s WHERE concat('',:name) like

Issue 3 - Update statements

Furthermore, I got a warning when creating the following update statement:
UPDATE Item i SET i.container = :container WHERE i = :item and i.belongsto = :person
The warning was:
“Input parameters can only be used in the WHERE clause or HAVING clause of a query.”
Apparently JPQL doesn't allow named parameters (or parameters of any kind) outside a WHERE or HAVING clause.

This was only a warning and the thing does work, but I should heed it.

It is described in Chapter JPQL Input Parameters of the JPQL Language Spec7


I especially liked the fact that in Netbeans, I can mouse over the NamedQueries in the Entities and it tells me in a popup what is wrong.


[1] The Java EE 7 Tutorial - Introduction to the Java Persistence API
[2] AdditionalCriteria
[3] AdditionCriteria
[4] StackOverflow - Is there a way to disable additionalcriteria in EclipseLink?
[5] StackOverflow - JPA How to persist column with sha1 encryption
[6] EclipseLink UserGuide - Support for Native Database Functions
[7] Oracle - JPQL Language References

Tuesday, 22 July 2014

Venn Diagrams

At work I seem to be the authority on the presence (or absence) of a colleague of mine who is much requested within the company for his domain model knowledge. Let's call him Hans, for that is his name.

The reason I am an authority is because I always walk with him from the train station to work.

A fact is that we both have long hair.

Hence a colleague of mine assumed that Long hair is a requirement for people who work here and go by train.

Venn Diagrams

So we have the following sets:
  • people who go by train
  • people who have long hair
  • people who work at my company


As I pointed out, the people my colleague, Bart, knows who work at my company and travel by train, also happen to have long hair. But the people my colleague knows is just a subset of people at my company. So let's add the set "people who Bart knows". So we have the following sets:

  • people who go by train
  • people who have long hair
  • people who work at my company
  • people who Bart knows

Now, we run into a little problem with Venn diagrams. As always, Wikipedia[1] to the rescue.

It's not possible to show all possibilities using regular circles, when the sets increase to more than three. In our case, for four possibilities, we can use ellipses.

Euler Diagrams[2]

Venn Diagrams are a Subset of Euler Diagrams, because Euler Diagrams are a bit more flexible when it comes to showing only some instead of all possible intersections.

The relationship between Venn and Euler diagrams can be displayed as an Euler Diagram.

I'm going to stop now, before I break my brain on the recursion.


[1] Wikipedia - Venn Diagram
[2] Wikipedia - Euler Diagram

Saturday, 19 July 2014

Successfully completed Functional Programming Principles in Scala

I earned 97.7% with distinction.


One can tell that the subject matter was Academic, with a firm grounding in Mathematics, which appealed to me.

The assignments provided a lot of information on what is expected, so there are no surprises, but you do need to read carefully.

The one assigment that provided the most difficulty was assignment 6, regarding the discovery of Anagrams of a sentence.

I had to wrestle a bit with the Scala syntax. It's new for me.

I especially found foldLeft and foldRight counter-intuitive sometimes.

I learned a lot on the following topics, in no specific ordering.
Scala Programming Language
by the creator, Martin Odersky, himself.
Functional Programming
one of the main subjects of the course
Domain Specific Language
Scala provides several ways to program according to a domain model2, instead of a technical/software model
Mathematics - Set Theory
the code is very close to the mathematical theory. Purposefully crafted that way, of course. It means we can actually use mathematical operators (some of the time).
Behaviour Driven Development
you can write tests that read more naturally
Test Driven Development
assignments had to pass certain tests (that are unknown), so your own tests had better be complete/sufficient
we used a lot of recursion, you do not see that in "normal" programming languages.
It was a huge amount of fun to do, both to learn a new Programming Language1 and to learn a new Programming Paradigm.


[1] Coursera - Functional Programming Principles in Scala, by Martin Odersky
[2] Wikipedia - Domain Model

Sunday, 13 July 2014

Migrating between JEE versions

I had an issue where I wanted to port my current JEE 6 (Web profile) application (YPPO) to JEE 7 (Full profile).

This concerns a Netbeans project (built using Ant)

I thought I'd just write down which files needed changing.
-<persistence version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
+<persistence version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
-<beans xmlns=""
+<beans xmlns=""
- xsi:schemaLocation="">

+ xsi:schemaLocation="" bean-discovery-mode="annotated">
-<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
+<web-app version="3.1" xmlns="" xmlns:xsi="" xsi:schemaLocation=""></dd>


Github - changed from JEE 6 Web (Lite) to JEE 7 (Full).
Github - changed from JEE 6 Web (Lite) to JEE 7 (Full) Part II.
Github - bean-discovery-mode required attribute in beans.xml

Monday, 7 July 2014

URI vs. URL vs. URN

What's a URI?

Uniform Resource Identifiers (URIs) are used to identify 'names' or 'resources'. They come in 2 varieties: URNs and URLs. In fact, a URI can be both an URN and an URL!

What's a URL?

Uniform Resource Locators (URLs) provide a way to locate a resource using a specific scheme, most often but not limited to HTTP. Just think of a URL as an address to a resource, and the scheme as a specification of how to get there.

What's a URN?

Uniform Resource Names are identifiers for resources. They are location independent and make use of the urn: scheme.

What's the syntax of a URI?


Examples from the RFC:

What's the syntax of a URL?



What's the syntax of a URN?


Examples from Wikipedia:

What's an XML Namespace?


An XML Namespace name is a uniform resource identifier (URI). However, the namespace specification does not require nor suggest that the namespace URI be used to retrieve information; it is simply treated by an XML parser as a string.

In general users should assume that the namespace URI is simply a name, not the address of a document on the Web.

Updated: 2014-09-18, added XML Namespace note.

Stackoverflow - What is the difference between a uri and a url
IETF - RFC3986
Wikipedia - XML namespace