Yaml-Fixtures for play 2 1

Posted by Jens Jäger on December 30, 2013

Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run.

The fixture implementation for loading testdata in play 1 was great. But all fixture related code for play 2 I found was ebean releated. After some research I decided to use Fixy for loading test data. It needed only some small changes to adapt it for play 2.

I added the code as a module to play4jpa. To use it, just copy the module to your project and add the necessary config to your Build.scala.

A yaml fixture file in the play conf folder can be loaded inside a transaction with this code:

The yaml for a Task Model with a name an done flag would look like this:

Check out the samples in the fixy unit tests on github for more details on using fixy.

A Ebean like finder for JPA 1

Posted by Jens Jäger on December 29, 2013

After the decision to go for JPA instead of Ebean with a Play Framework project, the first problem to solve was a replacement for the ebean finder.

The project team started with these simple requirements:

  • Jpa based
  • Easy to use
  • Can return a finder object, to add restrictions outside of the model (Used for generic components like the sort function of a data table view)

After some prototyping with different implementations, we found this gist and decided to start from there with Hibernate Criteria.

You can find the results in the query package and the generic model of my Play4Jpa project. The GenericModel doesn’t contain a generated long based @Id. You should only extend from GenericModel when you use combined keys. If you need a long based @Id you can extend from Model.

To get a query object from a Object that extends Model. You just have to add the method:

To find all Tasks with the query you just need to add the following findAll Method:

Here is a example of a simple where condition:

Play 2.x WS API and self signed certificates 5

Posted by Jens Jäger on December 03, 2013

The play web service API provides an easy way to use your play application as an http client. Usually you should use Play’s asynchronous mechanisms to use the web service response. But sometime you just want to wait for the web service responce (in a job). You can do this with the following method:

When the web service you want to call uses self signed certificates you will run into an exception:

To allow self-signed ssl certificates you just have to add the following to your application.conf.

When you unit test your web service call make sure your test runs inside a FakeApplication.

Play framework conference

Posted by Jens Jäger on December 02, 2013

Just bought a ticket to Ping Play! Edition. A 2-day by-the-community and for-the-community conference about the Play Framework.

Anybody knows a good hotel near BMC?

ping-logo-play@2x

Replace Play @Transactional with something better 10

Posted by Jens Jäger on November 30, 2013

One of the jpa issues I found in play 2 is the behaviour of the transaction handling.

The default play @Transactional does some kind of magic commit.

Lets have a look at the following controller action.

The problem is the variable task.name is changed in the hibernate persistence context and the change is saved on transaction commit. Of course you can use @Transactional(readOnly = true). But it’s easy to get this wrong.

Of course using readonly = true is not possible on actions they might only update a model under some conditions. In this case you might just confuse a = with == and you could have a nasty data changing bug in your application.

A much better approach would be: Only commit a transaction when an explicit save, update or delete is called from your model. The best way to realize this is a needsCommit thread local variable. The variable will be set to false by the transaction wrapper and to true inside the model methods.

The annotation an the call would look like the original play solutions:

The implementation of the withTx method looks like this:

In your model you need to set the commit variable to true like this:

Now you have a much more solid solution for your transactional handling. That only commits when you call a save in your model.

You find the code in the play4jpa project on github.