What’s New In simple-jpa 0.6?

One of the major changes in simple-jpa 0.6 is upgrade from JPA 2.0 to 2.1. This allow the implementation of generate-schema and fetchGraph/loadGraph in finders config. The next major change is @Transaction now can be used in any classes inside domain packages. This means any domain classes can act as repository if they have @Transaction on them. In the previous version, only Griffon’s artifacts such as controllers and services that can have dynamic finders on them.

The new generate-schema command will generate database objects to database or SQL scripts based on current domain models. For example, to drop existing tables and recreate new tables based on current domain models to JDBC connection defined in persistence.xml, you can use the following command:

griffon generate-schema -target=database -action=drop-and-create

By default, projects that use simple-jpa will recreate database objects (such as tables and its foreign keys) when they are launched. This mean generating database objects to database manually is unusual.

It is more typical to use generate-schema to generate SQL scripts that can be executed on another database (such as production database). generate-schema can generate two SQL scripts, one for drop statements and another one for create statements. For example, you can use the following command to generate SQL scripts based on current domain models:

griffon generate-schema -target=script -action=drop-and-create
                        -dropTarget=drop.sql -createTarget=create.sql

The command above will generate two files in current project directory: drop.sql containing SQL drop statements and create.sql containing SQL create statements.

simple-jpa now supports JPA 2.1 entity graphs. To use entity graphs, you must define a named entity graph (using @NamedEntityGraph annotation) or define it programmatically. For example, the following entity declares a named entity graph called StudentScores:

@DomainClass @Entity @TupleConstructor
@ToString(excludes = 'scores')
    attributeNodes = [@NamedAttributeNode('scores')]
class Student {

    String nama

    List scores= []


By default, collections fetching strategy is lazy. For example, the following finder will only select from student table in database:


If you are passing the student entities outside transaction scope and trying to get their scores, you will get the famous org.hibernate.LazyInitializationException. To avoid that problem, you can instruct JPA provider to fetch scores for student (as defined in named entity graph) using the following code:

findAllStudent([loadGraph: 'StudentScores'])

simple-jpa 0.6 supports both fetchGraph and loadGraph. In Hibernate JPA, they do the same thing. This behaviour may be different in other JPA providers. For example, in EclipseLink, fetchGraph will not fetch unlisted attributes (making them lazy) while loadGraph will use fetchType specified in mapping.

To use dynamic finders in domain classes, they must be annotated with @Transaction in class level. For example, the following code implements repository pattern:

package domain.repository


class ProductRepository {

  public Produk save(Product product) {
     if (findProductByName(product.name)) {
        throw new DuplicateData(product)


Because simple-jpa always injects finders as public methods, the following code is also possible:

ProductRepository productRepo = new ProductRepository()
Product p = productRepo.findProductByName(name)

Perihal Solid Snake
I'm nothing...

Apa komentar Anda?

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: