0

Does anyone initialize data by putting some code to static constructor of some initailizing class or something like that? I wonder where is posibillity like this:

@EJB //some bean
public someDataClassOrFacade {
  static {
    if(DataFacade.checkIfThereIsNoExampleData()) {
     SomeDataClass sdc = new SomeDataClass();
     // (...) set data
     DataFacade.create(sdc);
    }
   }
} 

What do you think about this way to initialize data? I am not convinced about using SQL queries with maven SQL plugin, because it has low flexibility (assuming data structure can change, especially columns names). But, i not pretty sure when class will be loaded. But this is only my theoretical practice, maybe you're using something far better than that? Thanks.

Dawid Pura
  • 991
  • 9
  • 32

2 Answers2

0

You want to initialize the data as late as possible and in a controlled moment in my opinion, so static initializers are a no-no for me. It depends on what type of application it is where I would do the initialization. If there is a web layer I would probably do it with a web framework specific initialization method or with a servlet context listener. If there is only an EJB layer, I might do it in a singleton EJB annotated with @Startup as this thread I quick-googled shows:

EJB stateless - Private members initialisation

Community
  • 1
  • 1
Gimby
  • 5,095
  • 2
  • 35
  • 47
  • If you don't want to tie it to an application container, JPA can also be used outside of the container. So a straight Java class could be run from the command line without requiring it to be tied to the application expecting the data as well. – EdH Mar 04 '14 at 10:34
  • I will try, you're right about using singleton with @Startup annotation, but before i make this answer as a solution i would check this out. Thanks. – Dawid Pura Mar 04 '14 at 10:58
-1

I'd use something like Liquibase instead of an EJB. It is designed for database management.

EdH
  • 4,918
  • 4
  • 24
  • 34