Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "annotatedClasses" needed if there is @Entity?

Hello I'm building spring-hibernate application. Do i realy need configuration from below ?

    <property name="annotatedClasses">
        <list>
            <value>org.fixus.springer.model.User</value>
        </list>
    </property>

I've set annotation-driven in my root-context.xml

<mvc:annotation-driven />
<context:component-scan base-package="org.fixus.springer" />
<context:component-scan base-package="org.fixus.springer.model" />

Now shouldn't hibernate automaticly take everything from this packages with annotation @Entity and convert it to table ? As for now without annotatedClasses he won't create a table from a entity

like image 936
Fixus Avatar asked Mar 18 '12 08:03

Fixus


1 Answers

Use the docs, Luke!

[...]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>

As you can see you have a choice between defining all classes explicitly or only the package for scanning. <context:component-scan/> does not recognize Hibernate/JPA annotations and hence has no effect.

like image 116
Tomasz Nurkiewicz Avatar answered Oct 11 '22 12:10

Tomasz Nurkiewicz