为什么是“annotatedClasses"?如果有@Entity 需要吗? [英] Why is "annotatedClasses" needed if there is @Entity?

查看:24
本文介绍了为什么是“annotatedClasses"?如果有@Entity 需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <列表><value>org.fixus.springer.model.User</value></list></属性>

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

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

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

解决方案

使用 文档,卢克!

<块引用>

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

<属性名称=数据源"ref=数据源"/><属性名称="annotatedClasses"><列表><value>test.package.Foo</value><value>test.package.Bar</value></list></属性></bean>

<块引用>

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

<属性名称=数据源"ref=数据源"/><property name="packagesToScan" value="test.package"/></bean>

如您所见,您可以选择明确定义所有类或仅定义要扫描的包.<context:component-scan/> 不识别 Hibernate/JPA 注释,因此无效.

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

解决方案

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.

这篇关于为什么是“annotatedClasses"?如果有@Entity 需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆