0

The Hibernate property - hibernate.hbm2ddl.import_files - will only be fired when the - hibernate.hbm2ddl.auto - is set to create.

Is there any implementation to "change" that?

Not the way hibernate.hbm2ddl.import_files behaves... but a way to Hibernate to insert some data after the system was started (even if the tables are created). Executing a pure SQL script won't match my needs because the @Id @GeneratedValue won't be respected.

At the end what I need is a way for the system to detect if some data is present at the database and if not, fill it with it. Is there a known to do it and respect my @GeneratedValue counter?

BBacon
  • 2,456
  • 5
  • 32
  • 52

2 Answers2

2

You can just create an @ApplicationScoped managed bean that calls a method that inserts the data in the database.

@ManagedBean(eager = true)
@ApplicationScoped
public class App {

    @PostConstruct
    public void init() {
        myDAO.initDatabaseIfNeeded();
    }
}

The method myDao.initDatabaseIfNeeded() is called when the application is initialized and after any dependencies are resolved -- thus after any tables have been created by Hibernate already.

I recently did a similar thing on a Spring application, so in my case myDAO was actually a Spring service.

Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
2

In addition to the right answer proposed by elias you could as well add a ServletContextListener, as it is probably a clearer way to achieve your functionality. To do so, you need to implement ServletContextListener interface and annotate the class with @WebListener, or declare it in your web.xml.

public class MyServletContextListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
    initializeDatabase();
}

@Override
public void contextDestroyed(ServletContextEvent sce) {   }

}

Then you either have to add a declaration in your web.xml:

<listener>
    <listener-class>yourpackage.MyServletContextListener</listener-class>
</listener>

or, if you're currently on Servlet 3.0, just annotate your class with @WebListener.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • +1 that's cleaner indeed! :) – Elias Dorneles Apr 04 '13 at 12:05
  • @elias At the same time I've already upvoted your answer as it's definitely much more clever :) Also, I was really amused how trickish a known situation may be turned out to be! – skuntsel Apr 04 '13 at 13:11
  • right! I should know the APIs better. :) When I used this trick, though, I think I really wanted an application scoped bean because it would also set a few properties with messages that were later displayed in the views -- so that was convenient. – Elias Dorneles Apr 04 '13 at 16:41
  • Your trick could be very useful if you wanted to have something initialized and stored in application scope, i.e. not only for a database initialization or for executing some other standalone function. For the latter the more proper place to run would indeed be the servlet context listener methods. – skuntsel Apr 05 '13 at 06:49