【问题标题】:Why is "annotatedClasses" needed if there is @Entity?如果有@Entity,为什么需要“annotatedClasses”?
【发布时间】:2012-04-03 03:45:25
【问题描述】:

您好,我正在构建 spring-hibernate 应用程序。我真的需要从下面配置吗?

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

我在 root-context.xml 中设置了注解驱动

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

现在 hibernate 不应该自动从带有注释 @Entity 的包中获取所有内容并将其转换为 table 吗?至于现在没有 annotatedClasses 他不会从实体创建表

【问题讨论】:

    标签: spring hibernate annotations


    【解决方案1】:

    使用docs,卢克!

    [...]AnnotationSessionFactoryBean bean 定义示例:

    <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>
    

    或者当使用类路径扫描来自动检测实体类时:

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

    如您所见,您可以选择显式定义所有类或仅定义要扫描的包。 &lt;context:component-scan/&gt; 无法识别 Hibernate/JPA 注释,因此没有效果。

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2020-03-07
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多