Samstag, 29. Juni 2013

Spring, JPA and Lazy fetch

Your task is to add a relation one-to-many to your entity class.

public class User {
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    List<Account> accounts;

Take a look on FetchType. In this case (LAZY) you have to fetch the collection in same session as the entity object. But Spring closes a session before. All you need is to implement a method in service layer and annotate it @Transactional(propagation=Propagation.REQUIRED). In such case Spring does all the operations inside the method in one session. For initialization just call some method (e.g. size()) on collection.

P.S.: Do NOT forget to switch on annotations in your configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <!-- ... -->

    <!-- ... -->


Sonntag, 9. Juni 2013

CoffeeScript compilation in Maven project

There are three available Maven plugins:
Brew works and has a lot of features, but unfortunately it is not supported yet.
Coffescript-maven-plugin works only with Java version 7, what in our case is not applicable.
Coffee-maven-plugin by talios has been recently updated und works fine with Maven 3 and Java 6.