Use entityunit in functional test

* Get rid of plain sql which is hard to maintain.
* Less problem with hibernate second level cache returning obsolete data when operating directly to database.
* Improve a few areas such as using jndi to look up document storage path etc.