Search code examples
playframework-2.0listenerebean

PlayFramework, how to register a BeanPersistListener?


I discovered the BeanPersistListener that I'd like to register to some of my models, but I didn't find any documentation, from Ebean nor PlayFramework on how to integrate it.

From the documentation :

A BeanPersistListener is either found automatically via class path search or can be added programmatically via ServerConfiguration.addEntity().

Apparently, it isn't found automatically (I added some Logger.info in the implemented methods, and nothing was shown), so I'd like to add it via ServerConfiguration, but how? where?

I'm also suspecting it's a version problem. From what I've seen, BeanPersistListener is from Ebean 2.6.0, but I can't find which version of Ebean PlayFramework is running (I'm using 2.0.4).


Solution

  • First Play 2.0.4 uses Ebean version 2.7.3 [1]

    And for registering your listener, you could try to use a ServerConfigStartup as shown in this documentation:

    package models;
    
    import com.avaje.ebean.config.ServerConfig;
    import com.avaje.ebean.event.ServerConfigStartup;
    import com.avaje.ebean.event.BeanPersistListener;
    
    public class MyServerConfigStartup implements ServerConfigStartup {
        @Override
        public void onStart(ServerConfig serverConfig) {
            serverConfig.add(new BeanPersistListener() {
                ....
            });
        }
    }
    

    I never tested it, I think it is worth a try :-)

    Update from comments:

    You'll need to put these classes under the models package or a child of the models package so that Ebean can find them.