Hibernate Mapping Package Hibernate Mapping Package java java

Hibernate Mapping Package


Out of the box - no. You can write your own code to detect / register your annotated classes, however. If you're using Spring, you can extend AnnotationSessionFactoryBean and do something like:

@Overrideprotected SessionFactory buildSessionFactory() throws Exception {  ArrayList<Class> classes = new ArrayList<Class>();  // the following will detect all classes that are annotated as @Entity  ClassPathScanningCandidateComponentProvider scanner =    new ClassPathScanningCandidateComponentProvider(false);  scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));  // only register classes within "com.fooPackage" package  for (BeanDefinition bd : scanner.findCandidateComponents("com.fooPackage")) {    String name = bd.getBeanClassName();    try {      classes.add(Class.forName(name));    } catch (Exception E) {      // TODO: handle exception - couldn't load class in question    }  } // for  // register detected classes with AnnotationSessionFactoryBean  setAnnotatedClasses(classes.toArray(new Class[classes.size()]));  return super.buildSessionFactory();}

If you're not using Spring (and you should be :-) ) you can write your own code for detecting appropriate classes and register them with your AnnotationConfiguration via addAnnotatedClass() method.

Incidentally, it's not necessary to map packages unless you've actually declared something at package level.


I just come across this problem, and find out there seems an out of box solution for this. My integration is not out yet, will update this later.

From the Javadoc, especially the packagesToScan part:

org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

Subclass of Spring's standard LocalSessionFactoryBean for Hibernate, supporting JDK 1.5+ annotation metadata for mappings.

Note: This class requires Hibernate 3.2 or later, with the Java Persistence API and the Hibernate Annotations add-on present.

Example for an AnnotationSessionFactoryBean bean definition:

<bean id="sessionFactory"       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  <property name="dataSource" ref="dataSource"/>  <property name="annotatedClasses">    <list>      <value>test.package.Foo</value>      <value>test.package.Bar</value>    </list>  </property></bean>

Or when using classpath scanning for autodetection of entity classes:

<bean id="sessionFactory"      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  <property name="dataSource" ref="dataSource"/>  <property name="packagesToScan" value="test.package"/></bean>

Since: 1.2.2
Author: Juergen Hoeller


Bit of thread necromancy here...

It still doesn't look like there's a good way to do what you want. If you don't want to use Spring, here's a similar method using Reflections:

// Create your SessionFactory with mappings for every `Entity` in a specific packageConfiguration configuration = new Configuration();configuration.configure("your_hibernate.cfg.xml");Reflections reflections = new Reflections("your_package");Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);for(Class<?> clazz : classes){    configuration.addAnnotatedClass(clazz);}ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

The same approach is probably viable for other Java reflection libraries.