Getting Started: EJB/Persistence (with PostgreSQL)

[DRAFT… To be continued …] last update: Feb 4th

I’ve been using PHP (especially Symfony 2.x) for quite a while in my web projects (both backend and frontend), coupled with MongoDB.

Almost a year ago, I’ve discovered that PostGreSQL supports “JSON/JSONB” and handles “requests” over Json/JsonB. Couple of months ago, I had a project involving OptaPlanner, so I had to move to J2EE world, and it not so worst.

A month ago, I started a project (on my free time) from scratch to deeply evaluate pros/cons of EJB/JPA/… with Wildfly 9.x.

So here, I will not discuss pros/cons neither compare it with PHP Symphony,  Python Django, or even Ruby on Rails; just give couple of Tips/Tricks around “Getting Started”

  1. Install : PostgreSQL & Wildfly
    – For my PoC/testing (Wildfly+Postgresql) server, I use a VPS (see OVH VPS for example: 1 Go RAM, 128Mo SWAP, 10Go HD)
    – Install Java on your server
    – Install PostgreSQL (9.4 or above) and starts it
    – Download Wildfly, uncompress, and runs it (here in standalone mode, listening all interfaces).

    bin/standalone.sh -b=0.0.0.0

  2. Configure WildFly DataSource
    This is a critical path: configure WildFly datasources. By default, an “ExampleDS”, using H2 memory database, is configured.
    Have a look over here (quick tutorial).
    There are plenty of ways to configured a PostgreSQL datasource using WildFly Web Administration, command-line client or simply “standalone.xml” edit.
  3. Setup a maven project
    In fact, this is where I’ve spent a lot of times: get a predefined maven project, from which I could start my tests/devs.
    Here is my pom.xml

  4. 1st tip : a generic “Reporitory” class
    As I did in a previous post regarding Symfony and a kind of “super” controller, I quickly had the same approach with Persistence/EJB. I created a top repository class to quickly handle simple stuff with @Entity.
    Here is my (old) very first but efficient AARootRepo.java
  5. 2nd tip/reminder: Hibernate key events/annotations
    Type Description
    @PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
    @PreRemove Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
    @PostPersist Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed.
    @PostRemove Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
    @PreUpdate Executed before the database UPDATE operation.
    @PostUpdate Executed after the database UPDATE operation.
    @PostLoad Executed after an entity has been loaded into the current persistence context or an entity has been refreshed.

    These events are quite useful to handle “json” data embedded into a blob/text field (through PostLoad/PreUpdate/PrePersist) for example, or to automatically update a field on persist/update (PrePersist/PreUpdate).