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

  • AnnotationSessionFactoryBean.packagesToScan - scanning of sub-packages?

    Hello,

    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:

    Code:
    <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">
    		<value>
    			hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    			hibernate.show_sql = true
    			hibernate.format_sql = true 
    		</value>
    	</property>
    </bean>
    But when I start my app and try to persist the class "de.moose.r2.domain.protocol.Person", I get the following exception:

    Code:
    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: 
    de.moose.r2.domain.protocol.Person
    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:
    com.model.Entity1
    com.model.Entity2
    com.model.foo.Entity3
    com.model.bar.Entity4
    com.model.foo.bar.Entity5
    com.model.bar.foo.Entity6

    My solution for this structure was:
    <property name="packagesToScan">
    <list>
    <value>com.model</value>
    <value>com.model.*</value>
    <value>com.model.*.*</value>
    </list>
    </property>

    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.

    Comment


    • #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:

      Code:
      <property name="packagesToScan">
      	<list>
      		<value>de.moose.r2.domain.**.*</value>
      	</list>
      </property>
      Now every entity-class, even in sub-packages, will be found by spring.

      Hope that helps

      Comment


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

        Thanks!

        Comment


        • #5
          Works like charm. Thanks!

          Comment

          Working...
          X