Sunday, December 25, 2016

Embedded entities and collections in Hibernate JPA

Sometimes the JPA model may differ from the database model. The JPA model may include embedded entities and embedded collections of entities.
The embedded entity is a Java class, which encapsulates mapping of a group of columns without binding to the particular table. The same embedded entity may be used in different JPA entities.

Sunday, December 18, 2016

My favorite JPA tutorial

I never worked with JPA and I had very tight schedule in the project. This JPA Tutorial - The ULTIMATE Guide was the best starting point I could imagine.
Even now, when I have certain experience with JPA, I always use it as a reference.
Of course nothing can replace the official hibernate documentation.

Saturday, December 10, 2016

Injecting Akka actors in Play with Guice

Actors are created by passing a Props instance into the actorOf factory method of the ActorSystem. This factory method will not work if an actor constructor has arguments, which should be injected.
Akka way for actors dependency injection is with IndirectActorProducer interface (see Akka documentation.).
If the dependency injection framework is Guice, Play makes some things under the veil and suggests another pattern for injecting Akka actors.

Saturday, December 3, 2016

Practical guide to Guice - part 2

This post sequels the previous one. It explains how to define multiple bindings, what is AssistedInject and how it may be used for dynamic injection.

Wednesday, November 30, 2016

Practical guide to Guice - part 1

This post is not yet another post agitating for dependency injection, which demonstrates Guice usage with a main method. It does not pretend to be a Guice tutorial either. If you are new for Guice or dependency injection Guice wiki page may be helpful.
This post explains how to implement dependency injection with Guice end-to-end and shows several practical examples.

Thursday, October 20, 2016

Scheduling asynchronous task in a Play application

Scheduling of asynchronous task in Play should be done with Akka actors. Play documentation about scheduling is rather laconic. It gives just code fragments and does not elaborate.
This post goes a bit further and shows basic but full example of scheduling of a periodic asynchronous task in a Play.

Sunday, October 9, 2016

Akka messages scheduling with Lambda actors

The official Akka documentation shows messages scheduling examples, which are implemented with the UntypedActor and hard-coded scheduling interval. This post demonstrates implementation of scheduling with Lambda actor and flexible interval.

Getting started Akka

I started to learn Akka and came up against several tutorials and fragments of code. Neither of them give the complete example of a simple "HelloWorld" project with the recent Akka release. This post should fill the gap: it gives complete examples of several simple Akka applications. It is recommended to have some reading on Akka documentation site before going on.

Tuesday, October 4, 2016

Examples of JPA attribute converters

Custom attribute conversion allows specify mapping between database and Java representation of an attribute. This may be done by implementing the JPA interface AttributeConverter. This post shows two examples of conversion:
1. A boolean Java attribute into an integer in DB
2. A String Java attribute into encrypted String in DB

Saturday, September 17, 2016

TIMESTAMP implementations with Hibernate JPA

Definition of the TIMESTAMP field may be done in different ways and it depends on a particular use case which method is more appropriate. The post gives several examples of TIMESTAMP implementation, but it does not pretend to explain all the options.

Saturday, September 10, 2016

NullAppender implementation for log4j2

The NullAppender, which exists in the log4j1, is not provided by the log4j2. In the log4j2 the NullAppender can be implemented as a plugin (see log4j2 manual).
The log4j1 documentation for the NullAppender states, that this is an appender, which merely exists, but never outputs a message to any device. This description was used for NullAppender plugin implementation presented in this post.

Sunday, August 28, 2016

Hibernate JPA OneToOne association with common primary key

Imagine, that you need to create a movies database, which stores a movie details and a details on a book, on which the movie was done. A book details are optional, since a movie may be not based on a book. A movie details are: title, release date, country, genre, language. A book details are: title, author, country, language.

You might define a table MOVIES that contains details for both a movie and a book. If a movie is not done on a book, the book-related columns will be null. For relational database such model means a waste of space, since a data column takes space according to its data type, no matter if data is null.

Monday, August 15, 2016

Updating Hibernate JPA entity collection

A detached entity (a.k.a. a detached object) is an object that has the same ID as an entity in the persistence storage but that is no longer a part of a persistence context (the scope of an EntityManager session). The two most common causes for this are:
• The transaction, in which the object was created, is closed.
• The object was received from outside as a part of a form submission, a remote protocol such as REST etc.

Tuesday, August 2, 2016

Using Guice for Play tests

The earlier post Unit testing and mocking with PowerMock in Play explains, how to define unit tests with the class WithApplication and Helpers.fakeApplication(). Such test is executed with the default injection binding, which is defined in a Play module. The default binding may be not appropriate for a particular test.

Wednesday, July 13, 2016

Dependency injection with Guice in Play

Guice is the default dependency injection (further DI) framework of Play. Other frameworks may be used as well, but using Guice makes development efforts easier, since Play takes care for things under the veil.
The official Play DI manual does not provide sufficient explanation. This post is the first in the sequence of posts, explaining in the different way and in more details, how DI with Guice should be done.

Saturday, July 2, 2016

Customizing JSON mapping in Play

Play implements JSON conversion on base of Jackson. Play's static API Json should be used for operating Json data.
For example:
// Convert from JSON:
MyClass person = Json.fromJson(json, MyClass.class);
// Convert back to JSON:
JsonNode jsonNode = Json.toJson(person);
The default JSON mapping may be changed by providing configuration to Jackon ObjectMapper.

Saturday, June 18, 2016

Unit testing in Play

Play testing may be done on different levels: unit tests, functional tests, server test etc. - as it is mentioned in Play testing manual. The focus in this post is on writing of unit tests.

Thursday, June 16, 2016

Mocking of generic types with PowerMock and Mockito

See the earlier post Mocking with PowerMock and Mockito how to use the PowerMock for mocking of static classes. Mocking of generic types demands some additional handling.

Let's consider an example of a class with static methods and generic types in API.

Mocking of enum singleton class with PowerMock and Mockito

The earlier post Mocking with PowerMock and Mockito explains how to use the PowerMock for mocking of static classes. Another method of mocking with the PowerMock is the Whitebox class. It provides various utilities for accessing internals of a class. A singleton class which is implemented with enum singleton pattern, may be easily mocked with the Whitebox.setInternalState.

In the example below a singleton DAOFactory contains several DAO services as enum values. Each DAO service supports DB access to a particular DB entity.

Sunday, June 12, 2016

How to attach CSV file to Play application response

Play makes it easy to manipulate a HTTPResponse. You can find in the manual, how to add headers, set cookies etc. in a response. This post shows how to attach a CSV file to a HTTPResponse.

The implementation differs according to the use case:
  • Some string source is converted to csv file while sending to the client.
  • An existing csv file is sent to the client.

Wednesday, June 8, 2016

Using eclipse as Play IDE

Working with Play may be done with a simple text editor and activator UI. Yet it is hard to imagine that this happens in practice. Play has several plugins for different IDE-s. The eclipse plugin allows to transform a Play project into an eclipse project with the command sbt eclipse.

Eclipse plugin may be set per project or globally per sbt user. It depends on team work, which approach should be used. If the whole team is using eclipse IDE, plugin may be set on a project level.

Monday, June 6, 2016

Using log4j2 with Play application

Play default logging engine is Logback. It may be combined with the another logging framework or replaced at all starting from Play 2.5.
This post complements the Play manual, which explains how to customize the Logback configuration for Scala, but does not refers Java.

Saturday, June 4, 2016

Customizing Play application configuration

By default a Play application configuration is defined with a file conf/application.conf. The default configuration file may be replaced or additional configuration files may be provided.

Wednesday, May 25, 2016

Migrating to Play 2.5 - can GlobalSettings be removed?

The post Migration from Play 2.4 to Play 2.5 explains the mandatory steps of migration to Play 2.5.
According to Migration guide removing of GlobalSettings implementation class is strongly recommended, but is optional. The exact recommendation is to move from it as much code as possible and in ideal to remove it at all.

In previous Play releases hooking of the application start-up, shut-down and request interception was done with a class Global, which extends the default GlobalSettings.
Such Global exists also in my project to be migrated to Play 2.5.

Monday, May 23, 2016

Migration from Play 2.4 to Play 2.5

The Play 2.5 migration guide gives general guidelines of what to do. As usual in Play documentation only few issues are really explained in details with "how to do".
This post fills the gaps (in scope of my experience in migrating the project).

Sunday, May 22, 2016

Making Play route case-insensitive - part 2

The previous post (part 1) explains, how to make the URL route case-insensitive.

If a URL contains some query parameters, additional treatment should be done in order to make the query parameters case insensitive as well.

Saturday, May 21, 2016

Making Play route case-insensitive - part 1

Play routes are case sensitive. It means, that if the route is defined like this:
 GET /thisIsMyPage       controllers.HomeController.exactRoute
the URL should match exactly the route including the letters case. Any attempt to provide different letter cases in the URL will result with NotFound error.

The one and only "good" URL is:
 localhost:9000/thisIsMyPage

Wednesday, May 18, 2016

Adding a custom folder to Play application distribution package

By default Play application distribution package is created as a directory tree, containing all necessary files for running the application.
A real application may demand adding more files or folders into the distribution.

Let's see, how to customize a Play application distribution package.

Friday, May 13, 2016

Integrating Play and AngularJs detached projects

Play and Angularjs are two frameworks for development of Web applications. The play-angular-seed template of the Typesafe activator suggests the incorporation of the Angularjs in Play.

There is another approach, suggesting to make development with the Angularjs outside the Play and integrate the both parts only in run-time.

Working with static html files in Play

Play comes with its own template engine, which compiles the Play views of type *.html.scala into static html files.

But what if we need to use instead of a Play view a plain html file?
For this purpose Play has a build-in static assets controller.

Wednesday, May 11, 2016

Creating of a maven project for Play application

While developing with Play/Java maven is more preferable, then sbt. It is the "native" Java tool and has a lot of plugins. Another reason to use maven - when a Play application is a part of a project, managed with maven.

Sunday, May 8, 2016

Getting started with Play Framework

Getting started with Play framework depends on the Play version. The official site describes the similar installation procedure for Play 2.4.x and almost all releases of Play 2.5. It suddenly changes to the different one starting in the couple of recent releases of Play 2.5.
Let's put all this to order.

Monday, April 25, 2016

Mocking with PowerMock and Mockito

The PowerMock extends Mockito and EasyMock frameworks. The vast advantage of the PowerMock is possibility to mock static and final classes and even private methods and members.

This post does not pretend to be a tutorial for the PowerMock. It just gives several examples of how-to-mock with PowerMock and Mockito.

Saturday, April 16, 2016

Testing log4j2 plugins with custom string appender

There is a bunch of appenders in log4j2 ready for use - visit this manual to get the impression.
A custom appender class is not demanded to be implemented as a plugin.

Wednesday, April 13, 2016

The log4j2 configuration naming and other tips

Customizing of log4j2 configuration is a big issue. This post gives just small tips for:
  • log4j2 configuration file naming
  • PatternLayout
  • File rolling

Sunday, April 10, 2016

Using log4j2 filter plugin for a separate errors log

The log4j2 filter controls output to the log. There is a plenty of ready for use filters as described in filters manual.

A filter allows or denies messages according to its filtering criteria. For example, the ThresholdFilter masks messages, which log level is above or beyond the threshold log level.

A filter activation is done with the log4j2 configuration.

Saturday, April 9, 2016

The log4j2 ThreadContext with configurable tags

Marking messages with application context tags has a great value for logs analyzing.
A context tag is some application-meaningful data, for example user id, specific application flow etc.

The log4j2 provides mechanism for aggregating tags in context of a thread with the ThreadContext.

Saturday, April 2, 2016

Extending log4j2 format with plugins

The log4j2 PatternLayout has the rich set of the build-in format specifiers and attributes.
A log file of desired format may be produced without a single line of code - just with a configuration file.
If the build-in format solutions are not sufficient for particular needs, the log4j2 plugins allow extending of the logging patterns.

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top