Using persistence out of a Play app

27 views
Skip to first unread message

A.

unread,
Feb 23, 2011, 4:30:33 AM2/23/11
to play-framework
Hi,

I've developped an application with Play Framework successfully (and
thank you for this great tool!).
I'm trying now to develop a batch program (i.e. a simple class with a
main method), in which I would like to use the Play persistence stack.

I've created a simple class in my project with a main method as
follow :

public static void main(String[] args) {
File root = new File(System.getProperty("application.path"));
Play.init(root, System.getProperty("play.id", ""));
System.out.println("users = " + User.count());
}

When I launch this class, I got the following error :
Please annotate your JPA model with @javax.persistence.Entity
annotation.

Of course my User class is correctly annotated ;)

I've checked out Play source code. Play.init() lauches
DBPlugin.onApplicationStart(), which do all the db setup stuff.
Do I miss something?
Thanks in advance.

PS : I know about Play jobs, but my problem is that this kind of batch
program should run out of a web application.

A.

unread,
Feb 25, 2011, 9:25:21 AM2/25/11
to play-framework
Hi,

I eventually found out the solution by myself, by looking at Play
source code.
The problem comes from the fact that Play uses its own classloader. If
you want to use Play persistence, your model classes need to be loaded
by Play classloader.

This example works fine :

public class Test {

public static void main(String[] args) throws Exception {

File root = new File(System.getProperty("application.path"));
Play.init(root, System.getProperty("play.id", ""));

Class c = Play.classloader.loadClass("jobs.Test$TestJob");
Job s = (Job) c.newInstance();
s.run();
}

public static class TestJob extends Job {
public void doJob() {

sas

unread,
Feb 25, 2011, 6:31:46 PM2/25/11
to play-framework
great candidate for a play snipet

http://www.playframework.org/community/snippets

Antoine Ansel

unread,
Feb 26, 2011, 2:25:17 AM2/26/11
to play-fr...@googlegroups.com
Ahh, I had never seen this page before!
Yes, that could be useful for others. I'll add it.



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Reply all
Reply to author
Forward
0 new messages