Announcement Announcement Module
No announcement yet.
AnnotationSessionFactoryBean.packagesToScan - scanning of sub-packages? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • AnnotationSessionFactoryBean.packagesToScan - scanning of sub-packages?


    I would like to use the "packagesToScan" class attribute of the AnnotationSessionFactoryBean, so that I do not have to declare all mapped entity classes manually. Please have a look at my "sessionFactory" bean:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
    	<property name="dataSource" ref="dataSource" />
    	<property name="packagesToScan" value="de.moose.r2.domain" />
    	<property name="hibernateProperties">
    			hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    			hibernate.show_sql = true
    			hibernate.format_sql = true 
    But when I start my app and try to persist the class "de.moose.r2.domain.protocol.Person", I get the following exception:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'hibernateTest' defined in file 
    [.....\server\HibernateTest.xml]: Invocation of init method failed; 
    nested exception is org.hibernate.MappingException: Unknown entity: 
    My guess is, that "packagesToScan" does not work recursively with sub-packages. I'm sure, that "de.moose.r2.domain" is correct.

    Does "packagesToScan" search for entity-classes in sub-packages? If yes, what might be the problem here?

    Thanks a lot for your help!

  • #2
    I am doing the same thing and ran into the same problem. As far as I can tell packagesToScan will take a list of packages and only scan for entities in those packages, not subpackages.

    I did have some success with the wildcard "*". Using an example class structure I will explain what I saw and what my solution was:

    My solution for this structure was:
    <property name="packagesToScan">

    com.model would find Entity1 and Entity2 and only those entities
    com.model.* would find Entity3 and Entity4 and only those entities
    com.model.*.* would find Entity5 and Entity6 and only those entities

    I haven't looked at the source code. I thought it would just be easier to play with it and see what it does.

    I think it would be nice if you could specify a .** to recurse through all subpackages. I don't know if the * is getting expanded by whatever parses the config or if the AnnotationSessionFactoryBean is supporting this.


    • #3
      Hi drob,

      thanks a lot for your answer. I think I found a solution, at least in my case. I just use this syntax:

      <property name="packagesToScan">
      Now every entity-class, even in sub-packages, will be found by spring.

      Hope that helps


      • #4
        I just tried it your way [**.*] and it worked great.



        • #5
          Works like charm. Thanks!