Preload data in database using JPA Hibernate.

251 views
Skip to first unread message

Jacco Steur

unread,
Jun 6, 2014, 9:12:36 AM6/6/14
to play-fr...@googlegroups.com
I am in the process of rewriting the play-authenticate-usage application from ebean to jpa hibernate.

The thing I got stuck with is that in the Global.java class of the app there is a preload of data for the security role. Because you can not get a JPA entitymanager in the Global.java context I need to do this in another way.

In unit test I use the fakeApplication stuff like:
running(fakeApplication(), new Runnable() {
public void run() {
JPA.withTransaction(new play.libs.F.Callback0() {
public void invoke() {
                                            JPA stuff.
};
});
}
});

}
 
I am not sure if this is the right way to go. What I really would like is using yaml to preload the database. Does anybody know how to dothis? In my final application I need to preload much more data then just the security_role 'user'.

Thanks in advance.

Endian Ogino

unread,
Jun 10, 2014, 12:02:49 PM6/10/14
to play-fr...@googlegroups.com
I actually doing it exactly like this.

 private void setupEntities() {
        JPA.withTransaction(() ->
     World firstWorld = ((List<World>) Yaml.load("default_fixtures/default-world.yml"))
                firstWorld.save(); 
        });
    }

With Play 2.3. + Java 8 it looks less ugly and it works.
My tests look also the same where I have extracted some boilerplate code into the BaseTest class

public void wrapRunnableAsTest(Runnable runnable) {
        running(testServer(PORT, app), runnable);
}

Thats the way I go. If anyone knows a better way, let me know.
Reply all
Reply to author
Forward
0 new messages