3

I am creating database tables by the following code:

public void createSchema() {
    Configuration configuration = new Configuration();
    configuration.setProperty("hibernate.connection.driver_class", driverClassName);
    configuration.setProperty("hibernate.connection.url", url);
    configuration.setProperty("hibernate.connection.username", username);
    configuration.setProperty("hibernate.connection.password", password);
    configuration.setProperty("hibernate.dialect", hibernateDialect);

    configuration.addAnnotatedClass(Base.class);
    configuration.addAnnotatedClass(User.class);
    logger.info("begin database schema creation =========================");
    new SchemaUpdate(configuration).execute(true, true);
    logger.info("end database schema creation ===========================");
}

Here I have specified the Annotated classes names by configuration.addAnnotatedClass method. If I have 50 classes, then it will be really messy.

Is there any way to specify packages names for scanning for Annotated classes?

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

2 Answers2

3

I'm not familiar with many of the changes in Hibernate 4, but there was no way to do this in Hibernate 3. I recommend using Spring's excellent Hibernate support, which lets you scan packages for entities, like you're asking for, and can manage a lot of other stuff for you, too, if you run your app in a Spring container.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
3

You can use Reflections to load all the Entities which are annotated as @Entity and add them to hibernate configuration. Chech the link fir a sample code Hibernate 4 Annotation Configuration

Maddy

Community
  • 1
  • 1
Maddy
  • 923
  • 5
  • 8